Trình kết nối cộng đồng hỗ trợ các phương thức xác thực sau:
- OAuth 2.0
- Đường dẫn/Tên người dùng/Mật khẩu
- Đường dẫn/Khoá
- Tên người dùng/Mật khẩu
- Tên người dùng/Mã thông báo
- Khoá
- Không có
Tuỳ thuộc vào phương thức bạn đang sử dụng, bạn phải cung cấp các hàm bổ sung trong trình kết nối.
Bảng sau đây cho biết những hàm mà bạn phải xác định tuỳ thuộc vào loại xác thực của trình kết nối.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
KHÔNG CÓ | |
---|---|---|---|
getAuthType() |
bắt buộc | bắt buộc | bắt buộc |
resetAuth() |
bắt buộc | bắt buộc | |
isAuthValid() |
bắt buộc | bắt buộc | |
authCallback() |
bắt buộc | ||
get3PAuthorizationUrls() |
bắt buộc | ||
setCredentials() |
bắt buộc |
getAuthType()
Hàm này sẽ trả về loại xác thực cho trình kết nối.
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
PHÍM
KHÔNG CÓ
resetAuth()
Hàm này sẽ xoá mọi thông tin đăng nhập được lưu trữ cho người dùng đối với dịch vụ của bên thứ ba.
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
PHÍM
isAuthValid()
Hàm này được gọi để xác định xem quá trình xác thực cho dịch vụ bên thứ ba có hợp lệ hay không. Nếu quá trình xác thực hợp lệ, thì các lệnh gọi đến getData()
và getSchema()
sẽ không thất bại do truy cập trái phép. Nếu quá trình xác thực không hợp lệ, thì người dùng có thể nhận được thông báo để bắt đầu quy trình uỷ quyền.
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
PHÍM
OAUTH2
Thêm và thiết lập OAuth2 cho Thư viện Apps Script
Làm theo hướng dẫn thiết lập cho thư viện OAuth2 cho Apps Script để thêm thư viện này vào dự án trình kết nối của bạn. Sau đó, hãy làm theo bước đầu tiên trong hướng dẫn sử dụng để tạo dịch vụ OAuth2 trong dự án trình kết nối. Dịch vụ OAuth2 của bạn có thể có bất kỳ tên hàm hợp lệ nào, nhưng hãy nhớ sử dụng cùng một tên khi tham chiếu đến dịch vụ OAuth2 trong mã của bạn.
Ví dụ: một dịch vụ OAuth2 có tên là exampleService
:
authCallback()
Hàm này được gọi để hoàn tất quy trình OAuth 2.0. Phản hồi gọi lại từ dịch vụ xác thực bên thứ ba được cung cấp dưới dạng một đối số và phải được hàm này xử lý.
Ví dụ về cách xử lý lệnh gọi lại OAuth 2.0 bằng thư viện OAuth2 cho Apps Script:
get3PAuthorizationUrls()
Hàm này được gọi để lấy URL cần thiết để bắt đầu quy trình uỷ quyền cho dịch vụ bên thứ ba. Nếu isAuthValid
trả về false
thì URL được trả về sẽ hiển thị cho người dùng dưới dạng nút hoặc đường liên kết để họ có thể uỷ quyền truy cập vào dịch vụ bên thứ ba. Xem tài liệu tham khảo về get3PAuthorizationUrls().
Ví dụ về cách trả về URL uỷ quyền bằng thư viện OAuth2 cho Apps Script:
USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
và PATH_KEY
Bạn chỉ cần thực hiện các bước sau cho quy trình xác thực USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
và PATH_KEY
.
setCredentials()
setCredentials
được gọi sau khi người dùng nhập thông tin về thông tin đăng nhập trên trang cấu hình trình kết nối cộng đồng. Bạn nên sử dụng Dịch vụ thuộc tính để lưu thông tin đăng nhập theo từng người dùng bằng cách sử dụng 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'
};
}