تتوافق موصِّلات المنتديات مع طرق المصادقة التالية:
- OAuth 2.0
- المسار/اسم المستخدم/كلمة المرور
- المسار/المفتاح
- اسم المستخدم/كلمة المرور
- اسم المستخدم/الرمز المميز
- المفتاح
- لا ينطبق
بناءً على الطريقة التي تستخدمها، يجب عليك تقديم دوال إضافية في الموصِّل.
يشير الجدول التالي إلى الدوال التي يجب أن تحددها بناءً على نوع المصادقة للموصل.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
NONE | |
---|---|---|---|
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
KEY
NONE
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
KEY
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
KEY
OAUTH2
إضافة OAuth2 وإعداده لمكتبة برمجة التطبيقات
اتّبِع تعليمات الإعداد لمكتبة OAuth2 لمكتبة "برمجة تطبيقات Google" لإضافتها إلى مشروع الموصِّل. بعد ذلك، اتّبِع الخطوة الأولى في دليل الاستخدام لإنشاء خدمة OAuth2 في مشروع الموصِّل. يمكن أن يكون لخدمة OAuth2 أي اسم وظيفة صالح، ولكن تأكَّد من استخدام الاسم نفسه عند الإشارة إلى خدمة OAuth2 في الرمز.
على سبيل المثال، خدمة OAuth2 باسم exampleService
:
authCallback()
يتم استدعاء هذه الدالة لإكمال مسار OAuth 2.0. يتم توفير استجابة معاودة الاتصال من خدمة المصادقة التابعة لجهة خارجية كوسيطة ويجب التعامل معها من خلال هذه الدالة.
مثال على التعامل مع معاودة الاتصال بـ OAuth 2.0 باستخدام مكتبة OAuth2 لمكتبة برمجة تطبيقات:
get3PAuthorizationUrls()
يتم استدعاء هذه الدالة للحصول على عنوان URL المطلوب لبدء تدفق المصادقة
للخدمة التابعة لجهة خارجية. إذا عرَضَ isAuthValid
الخطأ false
، سيتم عرض عنوان URL المعروض للمستخدم كزر أو رابط حتى يتمكّن من
السماح بالوصول إلى خدمة الجهة الخارجية. راجِع مرجع
get3PتفويضUrls().
مثال لعرض عنوان URL للتفويض باستخدام OAuth2 لمكتبة برمجة التطبيقات:
USER_PASS
وUSER_TOKEN
وKEY
وPATH_USER_PASS
وPATH_KEY
يجب استخدام ما يلي فقط لعمليات المصادقة USER_PASS
وUSER_TOKEN
وKEY
وPATH_USER_PASS
وPATH_KEY
.
setCredentials()
يتم استدعاء setCredentials
بعد إدخال المستخدم إما معلومات بيانات الاعتماد في صفحة ضبط موصل المنتدى. عليك استخدام
خدمة الخصائص لحفظ بيانات الاعتماد على أساس كل مستخدم
باستخدام 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'
};
}