Oprogramowanie sprzęgające społeczności obsługuje te metody uwierzytelniania:
- OAuth 2.0
- Ścieżka/nazwa użytkownika/hasło
- Ścieżka/klucz
- Nazwa użytkownika/hasło
- Nazwa użytkownika lub token
- Klucz
- Brak
W zależności od używanej metody musisz udostępnić w łączniku dodatkowe funkcje.
W tabeli poniżej znajdziesz informacje o tym, które funkcje musisz zdefiniować w zależności od typu uwierzytelniania oprogramowania sprzęgającego.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
BRAK | |
---|---|---|---|
getAuthType() |
wymagane | wymagane | wymagane |
resetAuth() |
wymagane | wymagane | |
isAuthValid() |
wymagane | wymagane | |
authCallback() |
wymagane | ||
get3PAuthorizationUrls() |
wymagane | ||
setCredentials() |
wymagane |
getAuthType()
Ta funkcja powinna zwracać typ uwierzytelniania oprogramowania sprzęgającego.
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
BRAK
resetAuth()
Ta funkcja usunie wszystkie dane logowania przechowywane dla użytkownika w usłudze innej firmy.
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()
Ta funkcja jest wywoływana, aby określić, czy uwierzytelnianie w usłudze innej firmy jest prawidłowe. Jeśli uwierzytelnianie jest prawidłowe, wywołania funkcji getData()
i getSchema()
nie powinny się nie powieść z powodu nieautoryzowanego dostępu. Jeśli autoryzacja jest nieprawidłowa, użytkownik może otrzymać powiadomienie o konieczności rozpoczęcia procesu autoryzacji.
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
Dodawanie i konfigurowanie OAuth2 w bibliotece Apps Script
Postępuj zgodnie z instrukcjami konfiguracji biblioteki OAuth2 for Apps Script, aby dodać ją do projektu łącznika. Następnie wykonaj pierwszy krok w przewodniku po korzystaniu z platformy, aby utworzyć usługę OAuth2 w projekcie łącznika. Usługa OAuth2 może mieć dowolną prawidłową nazwę funkcji, ale pamiętaj, aby używać tej samej nazwy podczas odwoływania się do usługi OAuth2 w kodzie.
Na przykład usługa OAuth2 o nazwie exampleService
:
authCallback()
Ta funkcja jest wywoływana w celu zakończenia procesu OAuth 2.0. Odpowiedź wywołania zwrotnego z usługi uwierzytelniania zewnętrznego jest przekazywana jako argument i powinna być obsługiwana przez tę funkcję.
Przykład obsługi wywołania zwrotnego OAuth 2.0 za pomocą biblioteki OAuth2 w Apps Script:
get3PAuthorizationUrls()
Ta funkcja jest wywoływana w celu uzyskania adresu URL, który jest wymagany do zainicjowania procesu autoryzacji w usłudze innej firmy. Jeśli funkcja isAuthValid
zwróci wartość false
, zwrócony adres URL zostanie wyświetlony użytkownikowi jako przycisk lub link, aby mógł on autoryzować dostęp do usługi innej firmy. Zobacz informacje o funkcji get3PAuthorizationUrls().
Przykład zwracania adresu URL autoryzacji za pomocą biblioteki OAuth2 dla Apps Script:
USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
i PATH_KEY
Poniższe informacje są potrzebne tylko w przypadku procesów uwierzytelniania USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
i PATH_KEY
.
setCredentials()
setCredentials
jest wywoływana po wpisaniu przez użytkownika informacji o danych logowania na stronie konfiguracji złącza społecznościowego. Do zapisywania danych logowania dla poszczególnych użytkowników za pomocą UserProperties
należy używać usługi Properties.
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'
};
}