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 che utilizzi, devi fornire funzioni aggiuntive nel connettore.
La tabella seguente indica le funzioni che devi definire a seconda del tipo di autenticazione del connettore.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
NESSUNO | |
---|---|---|---|
getAuthType() |
di provisioning. | di provisioning. | di provisioning. |
resetAuth() |
di provisioning. | di provisioning. | |
isAuthValid() |
di provisioning. | di provisioning. | |
authCallback() |
di provisioning. | ||
get3PAuthorizationUrls() |
di provisioning. | ||
setCredentials() |
di provisioning. |
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à 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, è previsto che le chiamate a
getData()
e getSchema()
non vadano a buon fine a causa di
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. Poi segui il primo passaggio della guida all'utilizzo per creare un servizio OAuth2 nel tuo progetto di connettore. Il tuo servizio OAuth2 può avere qualsiasi nome di funzione valido, ma assicurati di utilizzare lo stesso nome quando fai 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 dal 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à visualizzato all'utente come pulsante o link in modo che possa
autorizzare l'accesso al servizio di terze parti. Consulta 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 proprie credenziali
nella pagina di configurazione del connettore della community. Devi utilizzare il
servizio Properties per salvare le credenziali per 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'
};
}