การตรวจสอบสิทธิ์

เครื่องมือเชื่อมต่อชุมชนรองรับวิธีการตรวจสอบสิทธิ์ต่อไปนี้

  • 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()

ฟังก์ชันนี้จะล้างข้อมูลเข้าสู่ระบบที่จัดเก็บไว้สำหรับผู้ใช้สำหรับบุคคลที่สาม service.

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

เพิ่มและตั้งค่า OAuth2 สำหรับ Apps Script Library

ทำตามวิธีการตั้งค่า OAuth2 สำหรับ Apps Script เพื่อเพิ่มลงในโปรเจ็กต์เครื่องมือเชื่อมต่อ จากนั้นทำตามขั้นตอนแรกใน คู่มือการใช้งานเพื่อสร้างบริการ OAuth2 ในเครื่องมือเชื่อมต่อ บริการ OAuth2 จะมีชื่อฟังก์ชันที่ถูกต้อง ใช้ชื่อเดียวกันโดยอ้างอิงบริการ OAuth2 ในโค้ดของคุณ

ตัวอย่างเช่น บริการ OAuth2 ชื่อ exampleService:

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 ให้เสร็จสมบูรณ์ การตอบกลับการติดต่อกลับ จากบริการการตรวจสอบสิทธิ์ของบุคคลที่สามนั้นจะเป็นอาร์กิวเมนต์และ ด้วยฟังก์ชันนี้

ตัวอย่างการจัดการ Callback ของ OAuth 2.0 โดยใช้ OAuth2 สำหรับ Apps Script คลัง:

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()

ฟังก์ชันนี้เรียกใช้เพื่อรับ URL ที่ต้องใช้ในการเริ่มต้นการตรวจสอบสิทธิ์ สำหรับบริการของบุคคลที่สาม หาก isAuthValid แสดงผล false ระบบจะแสดงผล URL จะแสดงเป็นปุ่มหรือลิงก์แก่ผู้ใช้ เพื่อให้ผู้ใช้สามารถ ให้สิทธิ์เข้าถึงบริการของบุคคลที่สาม ดูข้อมูลอ้างอิงสำหรับ get3PAuthorizationUrls()

ตัวอย่างการส่งคืน URL การให้สิทธิ์โดยใช้ OAuth2 สำหรับ Apps Script คลัง:

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_PASS, USER_TOKEN, KEY, PATH_USER_PASS และ PATH_KEY

ต้องมีข้อมูลต่อไปนี้สำหรับ USER_PASS, USER_TOKEN, KEY เท่านั้น ขั้นตอนการตรวจสอบสิทธิ์ PATH_USER_PASS และ PATH_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'
 
};
}