Les connecteurs de communauté sont compatibles avec les méthodes d'authentification suivantes :
- OAuth 2.0
- Chemin/Nom d'utilisateur/Mot de passe
- Chemin d'accès/Clé
- Nom d'utilisateur/Mot de passe
- Nom d'utilisateur/Jeton
- Clé
- Aucun
Selon la méthode que vous utilisez, vous devez fournir des fonctions supplémentaires dans votre connecteur.
Le tableau suivant indique les fonctions que vous devez définir en fonction du type d'authentification de votre connecteur.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
AUCUN | |
---|---|---|---|
getAuthType() |
required | required | required |
resetAuth() |
required | required | |
isAuthValid() |
required | required | |
authCallback() |
required | ||
get3PAuthorizationUrls() |
required | ||
setCredentials() |
required |
getAuthType()
Cette fonction doit renvoyer le type d'authentification du connecteur.
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
JETON_UTILISATEUR
CLÉ
AUCUN
resetAuth()
Cette fonction effacera les identifiants stockés pour l'utilisateur pour le service tiers.
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
JETON_UTILISATEUR
CLÉ
isAuthValid()
Cette fonction est appelée pour déterminer si l'authentification du service tiers est valide. Si l'authentification est valide, les appels à getData()
et getSchema()
ne devraient pas échouer en raison d'un accès non autorisé. Si l'authentification n'est pas valide, l'utilisateur peut recevoir une notification pour démarrer le flux d'autorisation.
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
JETON_UTILISATEUR
CLÉ
OAUTH2
Ajouter et configurer OAuth2 pour la bibliothèque Apps Script
Suivez les instructions de configuration de la bibliothèque OAuth2 pour Apps Script afin de l'ajouter à votre projet de connecteur. Suivez ensuite la première étape du guide d'utilisation pour créer un service OAuth2 dans votre projet de connecteur. Votre service OAuth2 peut avoir n'importe quel nom de fonction valide, mais assurez-vous d'utiliser le même nom lorsque vous faites référence au service OAuth2 dans votre code.
Par exemple, un service OAuth2 nommé exampleService
:
authCallback()
Cette fonction est appelée pour terminer le flux OAuth 2.0. La réponse de rappel du service d'authentification tiers est fournie en tant qu'argument et doit être gérée par cette fonction.
Exemple de gestion du rappel OAuth 2.0 à l'aide de la bibliothèque OAuth2 pour Apps Script :
get3PAuthorizationUrls()
Cette fonction est appelée pour obtenir l'URL requise pour lancer le flux d'authentification du service tiers. Si isAuthValid
renvoie false
, l'URL renvoyée sera affichée à l'utilisateur sous la forme d'un bouton ou d'un lien afin qu'il puisse autoriser l'accès au service tiers. Consultez la documentation de référence pour get3PAuthorizationUrls().
Exemple de renvoi de l'URL d'autorisation à l'aide de la bibliothèque OAuth2 pour Apps Script :
USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
et PATH_KEY
Les informations suivantes ne sont nécessaires que pour les flux d'authentification USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
et PATH_KEY
.
setCredentials()
setCredentials
est appelé après que l'utilisateur a saisi ses informations d'identification sur la page de configuration du connecteur de communauté. Vous devez utiliser le service Properties pour enregistrer les identifiants pour chaque utilisateur à l'aide de 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'
};
}