מחברי הקהילה תומכים בשיטות האימות הבאות:
- 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
KEY
ללא
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 בספריית Apps Script
כדי להוסיף ספריית OAuth2 לספריית Apps Script, פועלים לפי הוראות ההגדרה לפרויקט המחבר. לאחר מכן, מבצעים את השלב הראשון במדריך השימוש כדי ליצור שירות OAuth2 בפרויקט המחבר. לשירות OAuth2 יכול להיות כל שם פונקציה חוקי, אבל עליכם להקפיד להשתמש באותו שם כשאתם מפנים לשירות OAuth2 בקוד שלכם.
לדוגמה, שירות OAuth2 בשם exampleService
:
authCallback()
הפונקציה הזו נקראת כדי להשלים את תהליך OAuth 2.0. תגובת הקריאה החוזרת משירות האימות של הצד השלישי מסופקת כארגומנט והפונקציה הזו צריכה לטפל בה.
דוגמה לטיפול בקריאה חוזרת (callback) של 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
לאחר שהמשתמש מזין את פרטי הכניסה שלו בדף התצורה של מחבר הקהילה. צריך להשתמש ב-Properties Service כדי לשמור את פרטי הכניסה על בסיס כל משתמש באמצעות 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'
};
}