社群連接器支援下列驗證方法:
- 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
為 Apps Script 程式庫新增及設定 OAuth2
按照 Apps Script 的 OAuth2 程式庫設定操作說明,將其新增至連接器專案。接著,按照使用指南中的第一個步驟,在連接器專案中建立 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
。您應使用屬性服務,使用 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'
};
}