ปลั๊กอินจากชุมชนสำหรับใช้ลิงก์ข้อมูลรองรับวิธีการตรวจสอบสิทธิ์ต่อไปนี้
- 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 Library
ทำตามวิธีการตั้งค่าสำหรับไลบรารี OAuth2 สำหรับ Apps Script เพื่อเพิ่มไลบรารีนี้ในโปรเจ็กต์เครื่องมือเชื่อมต่อ จากนั้นทำตามขั้นตอนแรกในคู่มือการใช้งานเพื่อสร้างบริการ OAuth2 ในโปรเจ็กต์เครื่องมือเชื่อมต่อ บริการ OAuth2 จะมีชื่อฟังก์ชันที่ถูกต้องได้ แต่โปรดตรวจสอบว่าใช้ชื่อเดียวกันขณะอ้างอิงถึงบริการ OAuth2 ในโค้ด
เช่น บริการ OAuth2 ชื่อ exampleService
authCallback()
ฟังก์ชันนี้เรียกว่าเพื่อให้ขั้นตอน OAuth 2.0 ดำเนินการให้เสร็จสมบูรณ์ การตอบสนองการเรียกกลับจากบริการการตรวจสอบสิทธิ์ของบุคคลที่สามจะมีการระบุเป็นอาร์กิวเมนต์และควรได้รับการจัดการโดยฟังก์ชันนี้
ตัวอย่างการจัดการโค้ดเรียกกลับของ 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'
};
}