Konektor komunitas mendukung metode autentikasi berikut:
- OAuth 2.0
- Jalur/Nama Pengguna/Sandi
- Jalur/Kunci
- Nama Pengguna/Sandi
- Nama Pengguna/Token
- Kunci
- Tidak ada
Bergantung pada metode yang digunakan, Anda harus menyediakan fungsi tambahan di konektor Anda.
Tabel berikut menunjukkan fungsi yang harus Anda tentukan bergantung pada jenis autentikasi konektor Anda.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
NONE | |
---|---|---|---|
getAuthType() |
wajib | wajib | wajib |
resetAuth() |
wajib | wajib | |
isAuthValid() |
wajib | wajib | |
authCallback() |
wajib | ||
get3PAuthorizationUrls() |
wajib | ||
setCredentials() |
wajib |
getAuthType()
Fungsi ini akan menampilkan jenis autentikasi untuk konektor.
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
NONE
resetAuth()
Fungsi ini akan menghapus semua kredensial yang disimpan bagi pengguna untuk layanan pihak ketiga.
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()
Fungsi ini dipanggil untuk menentukan apakah autentikasi layanan pihak ketiga valid. Jika autentikasi valid, panggilan ke
getData()
dan getSchema()
tidak akan gagal karena
akses tidak sah. Jika autentikasi tidak valid, pengguna mungkin akan menerima notifikasi untuk memulai alur otorisasi.
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
Menambahkan dan menyiapkan OAuth2 untuk Library Apps Script
Ikuti petunjuk penyiapan untuk library OAuth2 untuk Apps Script agar dapat menambahkannya ke project konektor Anda. Kemudian, ikuti langkah pertama dalam panduan penggunaan untuk membuat layanan OAuth2 di project konektor Anda. Layanan OAuth2 Anda dapat memiliki nama fungsi yang valid, tetapi pastikan untuk menggunakan nama yang sama saat merujuk ke layanan OAuth2 dalam kode Anda.
Misalnya, layanan OAuth2 bernama exampleService
:
authCallback()
Fungsi ini dipanggil untuk menyelesaikan alur OAuth 2.0. Respons callback dari layanan autentikasi pihak ketiga disediakan sebagai argumen dan harus ditangani oleh fungsi ini.
Contoh penanganan callback OAuth 2.0 menggunakan library OAuth2 untuk Apps Script:
get3PAuthorizationUrls()
Fungsi ini dipanggil untuk mendapatkan URL yang diperlukan guna memulai alur autentikasi untuk layanan pihak ketiga. Jika isAuthValid
menampilkan false
, URL
yang ditampilkan akan ditampilkan kepada pengguna sebagai tombol atau link sehingga mereka dapat
mengizinkan akses ke layanan pihak ketiga. Lihat referensi untuk get3PAuthorizationUrls().
Contoh menampilkan URL otorisasi menggunakan library OAuth2 untuk Apps Script:
USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
, dan PATH_KEY
Hal berikut hanya diperlukan untuk alur autentikasi USER_PASS
, USER_TOKEN
, KEY
,
PATH_USER_PASS
, dan PATH_KEY
.
setCredentials()
setCredentials
dipanggil setelah pengguna memasukkan informasi kredensial mereka di halaman konfigurasi konektor komunitas. Anda harus menggunakan
Properties Service untuk menyimpan kredensial per pengguna
menggunakan 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'
};
}