google-apps-script - Google Data Studio 连接器在第三方进行身份验证
问题描述
我正在为第三方源 Siteimprove 构建一个 Google Data Studio 连接器。Siteimprove 有一个需要Basic Access Authentication的 api 。
我已经在我的谷歌应用程序脚本中设置了用户名和令牌的身份验证(我也尝试过用户名和密码),所有必需的功能都基于文档
-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 响应,因此这似乎可以按预期工作。
有什么线索可以提示我为第三方服务提供凭据吗?
解决方案
/**
* 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_PASS)
.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() {
const usernameAndPassword = loadCurrentUsernameAndPassword();
return usernameAndPassword.username && usernameAndPassword.password && validateCredentials(usernameAndPassword.username, usernameAndPassword.password)
};
function loadCurrentUsernameAndPassword() {
const properties = PropertiesService.getUserProperties();
return {
username: properties.getProperty('dscc.username'),
password: properties.getProperty('dscc.password')
}
};
function setCredentials(request) {
var isCredentialsValid = validateCredentials(request.userPass.username, request.userPass.password);
if (!isCredentialsValid) {
return {
errorCode: "INVALID_CREDENTIALS"
};
} else {
storeUsernameAndPassword(request.userPass.username, request.userPass.password);
return {
errorCode: "NONE"
};
}
};
function validateCredentials(username, password) {
var rawResponse = UrlFetchApp.fetch('https://api.siteimprove.com/v2', {
method: 'GET',
headers: {
'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + password)
},
muteHttpExceptions: true
});
return rawResponse.getResponseCode() === 200;
}
function storeUsernameAndPassword(username, password) {
PropertiesService
.getUserProperties()
.setProperty('dscc.username', username)
.setProperty('dscc.password', password);
};
推荐阅读
- python - tfidf 矢量化器输出全部为零
- java - Java Selenium 测试 driver.close() 和 driver.quit() Firefox 问题
- python - 如何在 Pandas Dataframe 中组织 Selenium 中的字符串?
- hyperledger-fabric - 如何在超级账本结构中创建单一组织的网络?
- itext - 将pdf页面转换为itext中的图像
- android - 以编程方式更新后 Android 10 APK 重启
- typescript - Vuejs Typescript 类组件 refs.focus 不起作用
- c# - 不活动后asp.net超时,清晰度问题
- react-native - React Native 找不到变量:样式?
- google-analytics - 分析 UTM 标记显示加扰的活动和媒体信息