社群連結器支援下列驗證方法:
- 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
為 Apps Script 程式庫新增及設定 OAuth2
請按照 OAuth2 for Apps Script 程式庫的設定說明,將程式庫新增至連接器專案。然後按照使用指南中的第一個步驟,在連接器專案中建立 OAuth2 服務。OAuth2 服務可以使用任何有效的函式名稱,但請務必在程式碼中參照 OAuth2 服務時使用相同名稱。
舉例來說,名為 exampleService
的 OAuth2 服務:
authCallback()
這個函式用於完成 OAuth 2.0 流程。第三方驗證服務的回呼回應會以引數形式提供,且應由這個函式處理。
以下範例說明如何使用 Apps Script 適用的 OAuth2 程式庫處理 OAuth 2.0 回呼:
get3PAuthorizationUrls()
系統會呼叫這個函式,取得啟動第三方服務驗證流程所需的網址。如果 isAuthValid
傳回 false
,系統會向使用者顯示傳回的網址做為按鈕或連結,方便使用者授權存取第三方服務。請參閱 get3PAuthorizationUrls() 的參考資料。
以下範例說明如何使用 Apps Script 的 OAuth2 程式庫傳回授權網址:
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'
};
}