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