我正在为第三方来源Siteimprove构建一个Google Data Studio连接器。 Siteimprove有一个需要Basic Access Authentication的api。
我在我的谷歌应用脚本中为username and token设置了身份验证(我也尝试过用户名和密码),所有必需的功能都基于文档
-edit-按要求提供这些功能的完整代码
/**
* Returns the Auth Type of this connector.
* @return {object} The Auth type.
*/
function getAuthType() {
var cc = DataStudioApp.createCommunityConnector();
return cc.newAuthTypeResponse()
.setAuthType(cc.AuthType.USER_TOKEN)
.setHelpUrl('http://developer.siteimprove.com/v1/get-access/')
.build();
}
/**
* Resets the auth service.
*/
function resetAuth() {
var user_tokenProperties = PropertiesService.getUserProperties();
user_tokenProperties.deleteProperty('dscc.username');
user_tokenProperties.deleteProperty('dscc.password');
}
/**
* Returns true if the auth service has access.
* @return {boolean} True if the auth service has access.
*/
function isAuthValid() {
var userProperties = PropertiesService.getUserProperties();
var userName = userProperties.getProperty('dscc.username');
var token = userProperties.getProperty('dscc.token');
// This assumes you have a validateCredentials function that
// can validate if the userName and token are correct.
return validateCredentials(userName, token);
}
/**
* Sets the credentials.
* @param {Request} request The set credentials request.
* @return {object} An object with an errorCode.
*/
function setCredentials(request) {
var creds = request.userToken;
var username = creds.username;
var token = creds.token;
// Optional
// Check if the provided username and token are valid through a
// call to your service. You would have to have a `checkForValidCreds`
// function defined for this to work.
var validCreds = validateCredentials(username, token);
if (!validCreds) {
return {
errorCode: 'INVALID_CREDENTIALS'
};
}
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('dscc.username', username);
userProperties.setProperty('dscc.token', token);
return {
errorCode: 'NONE'
};
}
function validateCredentials(userName,token){
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(userName + ':' + token)
};
var params = {
"method":"GET",
"headers":headers
};
var response = UrlFetchApp.fetch("https://api.siteimprove.com/v2/", params);
return response;
console.log(response);
}
和清单文件
{
"dataStudio": {
"name": "Connector for Siteimprove",
"company": "<company name>",
"logoUrl": "<company logo url>",
"addonUrl": "",
"supportUrl": "",
"description": "This connector can be used to show basic data from Siteimprove"
}
}
当我运行脚本时,我会收到凭据提示,但这是一个连接谷歌帐户的提示
但我需要一种方法来为第三方服务提供凭据。如果我使用我的Google帐户,我会从Siteimprove API获得401响应,因此这似乎可以按预期工作。
如何获得提示为第三方服务提供凭据的任何线索?
Data Studio将始终根据您的脚本范围首先提示Google授权,然后根据您的getAuthType()
函数进行其他配置。由于您的getAuthType()
函数是USER_TOKEN
,在使用google授权后,您将有另外的提示来使用这些凭据进行授权。
codelab的The 4th step概述了连接器的流程,因此您可以看到何时调用哪些函数。
您还要确保至少定义了getAuthType()
,getData()
,getSchema()
和getConfig()
。由于您使用的是auth类型的USER_TOKEN
,因此必须按照Authentication中的说明定义其他方法。