I connettori della community supportano i seguenti metodi di autenticazione:
- OAuth 2.0
- Percorso/Nome utente/Password
- Percorso/Chiave
- Nome utente/password
- Nome utente/token
- Chiave
- Nessuno
A seconda del metodo utilizzato, devi fornire funzioni aggiuntive nel connettore.
La tabella seguente indica le funzioni da definire in base al tipo di autenticazione del connettore.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
NESSUNO | |
---|---|---|---|
getAuthType() |
obbligatorio | obbligatorio | obbligatorio |
resetAuth() |
obbligatorio | obbligatorio | |
isAuthValid() |
obbligatorio | obbligatorio | |
authCallback() |
obbligatorio | ||
get3PAuthorizationUrls() |
obbligatorio | ||
setCredentials() |
obbligatorio |
getAuthType()
Questa funzione deve restituire il tipo di autenticazione per il connettore.
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
NESSUNO
resetAuth()
Questa funzione cancellerà tutte le credenziali memorizzate per l'utente per il servizio di terze parti.
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()
Questa funzione viene chiamata per determinare se l'autenticazione per il servizio di terze parti è valida. Se l'autenticazione è valida, le chiamate a getData()
e getSchema()
non avranno esito positivo a causa di un accesso non autorizzato. Se l'autorizzazione non è valida, l'utente potrebbe ricevere una notifica per avviare il flusso di autorizzazione.
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
Aggiungere e configurare OAuth2 per la libreria Apps Script
Segui le istruzioni di configurazione per la libreria OAuth2 per Apps Script per aggiungerla al progetto del connettore. Quindi segui il primo passaggio nella guida all'utilizzo per creare un servizio OAuth2 nel progetto connettore. Il servizio OAuth2 può avere qualsiasi nome di funzione valido, ma assicurati di utilizzare lo stesso nome facendo riferimento al servizio OAuth2 nel codice.
Ad esempio, un servizio OAuth2 denominato exampleService
:
authCallback()
Questa funzione viene chiamata per completare il flusso OAuth 2.0. La risposta di callback del servizio di autenticazione di terze parti viene fornita come argomento e deve essere gestita da questa funzione.
Esempio di gestione del callback OAuth 2.0 utilizzando la libreria OAuth2 per Apps Script:
get3PAuthorizationUrls()
Questa funzione viene chiamata per ottenere l'URL necessario per avviare il flusso di autenticazione per il servizio di terze parti. Se isAuthValid
restituisce false
, l'URL
restituito verrà mostrato all'utente sotto forma di pulsante o link, in modo che possa
autorizzare l'accesso al servizio di terze parti. Vedi il riferimento per get3PAuthorizationUrls().
Esempio di restituzione dell'URL di autorizzazione utilizzando la libreria OAuth2 per Apps Script:
USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
e PATH_KEY
Quanto segue è necessario solo per i flussi di autenticazione USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
e PATH_KEY
.
setCredentials()
setCredentials
viene chiamato dopo che l'utente ha inserito le informazioni sulle credenziali nella pagina di configurazione del connettore della community. Devi utilizzare il Servizio proprietà per salvare le credenziali in base a ogni utente utilizzando 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'
};
}