تتيح موصلات المنتدى طرق المصادقة التالية:
- OAuth 2.0
- المسار/اسم المستخدم/كلمة المرور
- المسار/المفتاح
- اسم المستخدم/كلمة المرور
- اسم المستخدم/الرمز المميّز
- المفتاح
- بدون
استنادًا إلى الطريقة التي تستخدمها، يجب توفير وظائف إضافية في أداة الربط.
يوضّح الجدول التالي الدوال التي يجب تحديدها استنادًا إلى نوع المصادقة في الموصل.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
لم يتم اختيار لون | |
---|---|---|---|
getAuthType() |
مطلوب | مطلوب | مطلوب |
resetAuth() |
مطلوب | مطلوب | |
isAuthValid() |
مطلوب | مطلوب | |
authCallback() |
مطلوب | ||
get3PAuthorizationUrls() |
مطلوب | ||
setCredentials() |
مطلوب |
getAuthType()
يجب أن تعرض هذه الدالة نوع المصادقة للموصل.
OAUTH2
PATH_USER_PASS
/**
* Returns the Auth Type of this connector.
* @return {object} The Auth type.
*/
function getAuthType() {
var cc = DataStudioApp.createCommunityConnector();
return cc.newAuthTypeResponse()
.setAuthType(cc.AuthType.PATH_USER_PASS)
.setHelpUrl('https://www.example.org/connector-auth-help')
.build();
}
PATH_KEY
/**
* Returns the Auth Type of this connector.
* @return {object} The Auth type.
*/
function getAuthType() {
var cc = DataStudioApp.createCommunityConnector();
return cc.newAuthTypeResponse()
.setAuthType(cc.AuthType.PATH_KEY)
.setHelpUrl('https://www.example.org/connector-auth-help')
.build();
}
USER_PASS
USER_TOKEN
المفتاح
لم يتم اختيار لون
resetAuth()
ستؤدي هذه الدالة إلى محو أي بيانات اعتماد مخزَّنة للمستخدم في الخدمة التابعة لجهة خارجية.
OAUTH2
PATH_USER_PASS
/**
* Resets the auth service.
*/
function resetAuth() {
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty('dscc.path');
userProperties.deleteProperty('dscc.username');
userProperties.deleteProperty('dscc.password');
}
PATH_KEY
/**
* Resets the auth service.
*/
function resetAuth() {
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteProperty('dscc.path');
userProperties.deleteProperty('dscc.key');
}
USER_PASS
USER_TOKEN
المفتاح
isAuthValid()
يتم استدعاء هذه الدالة لتحديد ما إذا كانت المصادقة لخدمة الجهة الخارجية صالحة. إذا كانت المصادقة صالحة، من المتوقّع ألا يتعذّر تنفيذ طلبات getData()
وgetSchema()
بسبب الوصول غير المصرّح به. إذا لم تكن عملية المصادقة صالحة، قد يتلقّى المستخدم إشعارًا لبدء عملية التفويض.
OAUTH2
PATH_USER_PASS
/**
* Returns true if the auth service has access.
* @return {boolean} True if the auth service has access.
*/
function isAuthValid() {
var userProperties = PropertiesService.getUserProperties();
var path = userProperties.getProperty('dscc.path');
var userName = userProperties.getProperty('dscc.username');
var password = userProperties.getProperty('dscc.password');
// This assumes you have a validateCredentials function that
// can validate if the path, userName and password are correct.
return validateCredentials(path, userName, password);
}
PATH_KEY
/**
* Returns true if the auth service has access.
* @return {boolean} True if the auth service has access.
*/
function isAuthValid() {
var userProperties = PropertiesService.getUserProperties();
var path = userProperties.getProperty('dscc.path');
var key = userProperties.getProperty('dscc.key');
// This assumes you have a validateCredentials function that
// can validate if the path and key are correct.
return validateCredentials(path, key);
}
USER_PASS
USER_TOKEN
المفتاح
OAUTH2
إضافة وإعداد OAuth2 لمكتبة Apps Script
اتّبِع تعليمات الإعداد الخاصة بمكتبة OAuth2 لتطبيق Apps Script لإضافتها إلى مشروع الموصل. بعد ذلك، اتّبِع الخطوة الأولى في دليل الاستخدام لإنشاء خدمة OAuth2 في مشروع الموصل. يمكن أن يتضمّن اسم خدمة OAuth2 أي اسم دالة صالح، ولكن احرص على استخدام الاسم نفسه عند الإشارة إلى خدمة OAuth2 في الرمز البرمجي.
على سبيل المثال، خدمة OAuth2 باسم exampleService
:
authCallback()
يتم استدعاء هذه الدالة لإكمال عملية OAuth 2.0. يتم تقديم ردّ الاتصال من خدمة المصادقة التابعة لجهة خارجية كمعلَمة، ويجب أن تعالج هذه الدالة هذا الردّ.
مثال على معالجة معاودة الاتصال OAuth 2.0 باستخدام مكتبة OAuth2 لـ Apps Script:
get3PAuthorizationUrls()
يتم استدعاء هذه الدالة للحصول على عنوان URL المطلوب لبدء عملية المصادقة الخاصة بالخدمة التابعة لجهة خارجية. إذا عرضت isAuthValid
القيمة false
، سيتم عرض عنوان URL الذي تم عرضه للمستخدم على شكل زر أو رابط ليتمكّن من منح الإذن بالوصول إلى خدمة الجهة الخارجية. اطّلِع على المرجع الخاص بالدالة
get3PAuthorizationUrls().
مثال على عرض عنوان URL الخاص بالمصادقة باستخدام مكتبة OAuth2 لـ Apps Script:
USER_PASS
وUSER_TOKEN
وKEY
وPATH_USER_PASS
وPATH_KEY
لا يلزم توفير ما يلي إلا لعمليات المصادقة USER_PASS
وUSER_TOKEN
وKEY
وPATH_USER_PASS
وPATH_KEY
.
setCredentials()
يتم استدعاء setCredentials
بعد أن يُدخل المستخدم معلومات بيانات الاعتماد في صفحة إعداد أداة ربط Community Connector. يجب استخدام خدمة الخصائص لحفظ بيانات الاعتماد لكل مستخدم على حدة باستخدام UserProperties
.
PATH_USER_PASS
/**
* Sets the credentials.
* @param {Request} request The set credentials request.
* @return {object} An object with an errorCode.
*/
function setCredentials(request) {
var creds = request.pathUserPass;
var path = creds.path;
var username = creds.username;
var password = creds.password;
// Optional
// Check if the provided path, username and password are valid through
// a call to your service. You would have to have a `checkForValidCreds`
// function defined for this to work.
var validCreds = checkForValidCreds(path, username, password);
if (!validCreds) {
return {
errorCode: 'INVALID_CREDENTIALS'
};
}
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('dscc.path', path);
userProperties.setProperty('dscc.username', username);
userProperties.setProperty('dscc.password', password);
return {
errorCode: 'NONE'
};
}
PATH_KEY
/**
* Sets the credentials.
* @param {Request} request The set credentials request.
* @return {object} An object with an errorCode.
*/
function setCredentials(request) {
var creds = request.pathKey;
var path = creds.path;
var key = creds.key;
// Optional
// Check if the provided path and key are valid through
// a call to your service. You would have to have a `checkForValidCreds`
// function defined for this to work.
var validCreds = checkForValidCreds(path, key);
if (!validCreds) {
return {
errorCode: 'INVALID_CREDENTIALS'
};
}
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('dscc.path', path);
userProperties.setProperty('dscc.key', key);
return {
errorCode: 'NONE'
};
}