驗證

社群連接器支援下列驗證方法:

  • OAuth 2.0
  • 路徑/使用者名稱/密碼
  • 路徑/金鑰
  • 使用者名稱/密碼
  • 使用者名稱/權杖

視您使用的方法而定,您必須提供額外函式。 。

下表指出您必須根據 驗證類型

OAUTH2 PATH_USER_PASS
PATH_KEY
USER_PASS
USER_TOKEN
KEY
getAuthType() 必填 必填 必填
resetAuth() 必填 必填
isAuthValid() 必填 必填
authCallback() 必填
get3PAuthorizationUrls() 必填
setCredentials() 必填

getAuthType()

此函式應會傳回連接器的驗證類型。

data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */

function getAuthType() {
 
var cc = DataStudioApp.createCommunityConnector();
 
return cc.newAuthTypeResponse()
     
.setAuthType(cc.AuthType.OAUTH2)
     
.build();
}
/**
 * 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();
}
/**
 * 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();
}
data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */

function getAuthType() {
 
var cc = DataStudioApp.createCommunityConnector();
 
return cc.newAuthTypeResponse()
     
.setAuthType(cc.AuthType.USER_PASS)
     
.setHelpUrl('https://www.example.org/connector-auth-help')
     
.build();
}
data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */

function getAuthType() {
 
var cc = DataStudioApp.createCommunityConnector();
 
return cc.newAuthTypeResponse()
     
.setAuthType(cc.AuthType.USER_TOKEN)
     
.setHelpUrl('https://www.example.org/connector-auth-help')
     
.build();
}
data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */

function getAuthType() {
 
var cc = DataStudioApp.createCommunityConnector();
 
return cc.newAuthTypeResponse()
     
.setAuthType(cc.AuthType.KEY)
     
.setHelpUrl('https://www.example.org/connector-auth-help')
     
.build();
}
data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */

function getAuthType() {
 
var cc = DataStudioApp.createCommunityConnector();
 
return cc.newAuthTypeResponse()
     
.setAuthType(cc.AuthType.NONE)
     
.build();
}

resetAuth()

這個函式會清除為第三方使用者儲存的所有憑證 課程中也會快速介紹 Memorystore 這是 Google Cloud 的全代管 Redis 服務

data-studio/auth.gs
/**
 * Resets the auth service.
 */

function resetAuth() {
  getOAuthService
().reset();
}
/**
 * Resets the auth service.
 */

function resetAuth() {
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.deleteProperty('dscc.path');
  userProperties
.deleteProperty('dscc.username');
  userProperties
.deleteProperty('dscc.password');
}
/**
 * Resets the auth service.
 */

function resetAuth() {
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.deleteProperty('dscc.path');
  userProperties
.deleteProperty('dscc.key');
}
data-studio/auth.gs
/**
 * Resets the auth service.
 */

function resetAuth() {
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.deleteProperty('dscc.username');
  userProperties
.deleteProperty('dscc.password');
}
data-studio/auth.gs
/**
 * Resets the auth service.
 */

function resetAuth() {
 
var userTokenProperties = PropertiesService.getUserProperties();
  userTokenProperties
.deleteProperty('dscc.username');
  userTokenProperties
.deleteProperty('dscc.password');
}
data-studio/auth.gs
/**
 * Resets the auth service.
 */

function resetAuth() {
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.deleteProperty('dscc.key');
}

isAuthValid()

系統會呼叫此函式來判斷第三方是否進行驗證 服務有效。如果驗證有效,則應該呼叫 getData()getSchema() 不會失敗,原因如下: 未經授權存取。如果驗證無效,使用者可能會收到 開始授權流程的通知。

data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */

function isAuthValid() {
 
return getOAuthService().hasAccess();
}
/**
 * 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);
}
/**
 * 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);
}
data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */

function isAuthValid() {
 
var userProperties = PropertiesService.getUserProperties();
 
var userName = userProperties.getProperty('dscc.username');
 
var password = userProperties.getProperty('dscc.password');
 
// This assumes you have a validateCredentials function that
 
// can validate if the userName and password are correct.
 
return validateCredentials(userName, password);
}
data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */

function isAuthValid() {
 
var userProperties = PropertiesService.getUserProperties();
 
var userName = userProperties.getProperty('dscc.username');
 
var token = userProperties.getProperty('dscc.token');
 
// This assumes you have a validateCredentials function that
 
// can validate if the userName and token are correct.
 
return validateCredentials(userName, token);
}
data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */

function isAuthValid() {
 
var userProperties = PropertiesService.getUserProperties();
 
var key = userProperties.getProperty('dscc.key');
 
// This assumes you have a validateKey function that can validate
 
// if the key is valid.
 
return validateKey(key);
}

OAUTH2

新增並設定 Apps Script 程式庫的 OAuth2

按照 Apps Script 的 OAuth2 設定操作說明進行 即可加入連接器專案。接著,按照 使用指南,在連接器中建立 OAuth2 服務 專案。您的 OAuth2 服務可以具有任何有效的函式名稱,但請務必 在程式碼中參照 OAuth2 服務時,使用相同的名稱。

例如,名為 exampleService 的 OAuth2 服務:

data-studio/auth.gs
/**
 * Returns the configured OAuth Service.
 * @return {Service} The OAuth Service
 */

function getOAuthService() {
 
return OAuth2.createService('exampleService')
     
.setAuthorizationBaseUrl('...')
     
.setTokenUrl('...')
     
.setClientId('...')
     
.setClientSecret('...')
     
.setPropertyStore(PropertiesService.getUserProperties())
     
.setCallbackFunction('authCallback')
     
.setScope('...');
};

authCallback()

呼叫此函式即可完成 OAuth 2.0 流程。回呼回應 則來自第三方驗證服務的結果 由此函式處理

使用 Apps Script 的 OAuth2 處理 OAuth 2.0 回呼的範例 程式庫:

data-studio/auth.gs
/**
 * The OAuth callback.
 * @param {object} request The request data received from the OAuth flow.
 * @return {HtmlOutput} The HTML output to show to the user.
 */

function authCallback(request) {
 
var authorized = getOAuthService().handleCallback(request);
 
if (authorized) {
   
return HtmlService.createHtmlOutput('Success! You can close this tab.');
 
} else {
   
return HtmlService.createHtmlOutput('Denied. You can close this tab');
 
};
};

get3PAuthorizationUrls()

系統會呼叫此函式,取得啟動驗證所需的網址 第三方服務流程如果 isAuthValid 傳回 false,則網址 系統將以按鈕或連結的形式向使用者顯示,方便他們 授權存取第三方服務。請參閱 get3PAuthorizationUrls()

使用 Apps Script 的 OAuth2 傳回授權網址的範例 程式庫:

data-studio/auth.gs
/**
 * Gets the 3P authorization URL.
 * @return {string} The authorization URL.
 * @see https://developers.google.com/apps-script/reference/script/authorization-info
 */

function get3PAuthorizationUrls() {
 
return getOAuthService().getAuthorizationUrl();
}

USER_PASSUSER_TOKENKEYPATH_USER_PASS以及PATH_KEY

以下項目只有 USER_PASSUSER_TOKENKEYPATH_USER_PASSPATH_KEY 驗證流程。

setCredentials()

使用者輸入憑證後,系統會呼叫 setCredentials 請參閱社群連接器設定頁面的資訊。您應該使用 屬性服務:儲存每位使用者的憑證 使用 UserProperties

/**
 * 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'
 
};
}
/**
 * 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'
 
};
}
data-studio/auth.gs
/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */

function setCredentials(request) {
 
var creds = request.userPass;
 
var username = creds.username;
 
var password = creds.password;

 
// Optional
 
// Check if the provided 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(username, password);
 
if (!validCreds) {
   
return {
      errorCode
: 'INVALID_CREDENTIALS'
   
};
 
}
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.setProperty('dscc.username', username);
  userProperties
.setProperty('dscc.password', password);
 
return {
    errorCode
: 'NONE'
 
};
}
data-studio/auth.gs
/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */

function setCredentials(request) {
 
var creds = request.userToken;
 
var username = creds.username;
 
var token = creds.token;

 
// Optional
 
// Check if the provided username and token are valid through a
 
// call to your service. You would have to have a `checkForValidCreds`
 
// function defined for this to work.
 
var validCreds = checkForValidCreds(username, token);
 
if (!validCreds) {
   
return {
      errorCode
: 'INVALID_CREDENTIALS'
   
};
 
}
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.setProperty('dscc.username', username);
  userProperties
.setProperty('dscc.token', token);
 
return {
    errorCode
: 'NONE'
 
};
}
data-studio/auth.gs
/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */

function setCredentials(request) {
 
var key = request.key;

 
// Optional
 
// Check if the provided key is valid through a call to your service.
 
// You would have to have a `checkForValidKey` function defined for
 
// this to work.
 
var validKey = checkForValidKey(key);
 
if (!validKey) {
   
return {
      errorCode
: 'INVALID_CREDENTIALS'
   
};
 
}
 
var userProperties = PropertiesService.getUserProperties();
  userProperties
.setProperty('dscc.key', key);
 
return {
    errorCode
: 'NONE'
 
};
}