Społecznościowe oprogramowanie sprzęgające 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/token
- Klucz
- Brak
W zależności od używanej metody musisz zapewnić w oprogramowaniu sprzęgającym dodatkowe funkcje.
W poniższej tabeli podano, które funkcje należy 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 dla 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 usuwa wszystkie dane logowania zapisane dla użytkownika na potrzeby usługi zewnętrznej.
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()
Funkcja ta jest wywoływana w celu określenia, czy uwierzytelnianie usługi zewnętrznej jest prawidłowe. Jeśli uwierzytelnianie jest prawidłowe, wywołania getData()
i getSchema()
nie będą kończyć się niepowodzeniem z powodu nieautoryzowanego dostępu. Jeśli uwierzytelnianie jest nieprawidłowe, użytkownik może otrzymać powiadomienie o rozpoczęciu 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 protokołu OAuth2 na potrzeby biblioteki Apps Script
Wykonaj instrukcje konfiguracji biblioteki OAuth2 dla Apps Script, aby dodać ją do projektu oprogramowania sprzęgającego. Następnie wykonaj pierwszy krok w przewodniku korzystania, aby utworzyć usługę OAuth2 w projekcie oprogramowania sprzęgającego. Usługa OAuth2 może mieć dowolną prawidłową nazwę funkcji, ale upewnij się, że używasz tej samej nazwy w odniesieniu do usługi OAuth2 w kodzie.
Na przykład usługa OAuth2 o nazwie exampleService
:
authCallback()
Funkcja ta jest wywoływana w celu ukończenia procesu OAuth 2.0. Odpowiedź wywołania zwrotnego z zewnętrznej usługi uwierzytelniania jest podawana jako argument i powinna być obsługiwana przez tę funkcję.
Przykład obsługi wywołania zwrotnego OAuth 2.0 za pomocą biblioteki OAuth2 dla Apps Script:
get3PAuthorizationUrls()
Funkcja ta jest wywoływana w celu uzyskania adresu URL wymaganego do zainicjowania procesu uwierzytelniania usługi zewnętrznej. Jeśli isAuthValid
zwraca wartość false
, zwrócony adres URL jest wyświetlany użytkownikowi jako przycisk lub link, który umożliwia autoryzowanie dostępu do usługi zewnętrznej. Zapoznaj się z dokumentacją 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 przepływach uwierzytelniania USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
i PATH_KEY
.
setCredentials()
Funkcja setCredentials
jest wywoływana po wpisaniu przez użytkownika swoich danych logowania na stronie konfiguracji oprogramowania sprzęgającego społeczności. Aby zapisywać dane uwierzytelniające dla poszczególnych użytkowników za pomocą UserProperties
, należy użyć usługi usług.
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'
};
}