google-apps-script - Google Data Studio 连接器在第三方进行身份验证

标签 google-apps-script looker-studio

我正在构建一个连接到第三方源 Siteimprove 的 Google Data Studio 连接器。 Siteimprove 有一个 api,需要 Basic Access Authentication .

我已经为 username and token 设置了身份验证(我还尝试过用户名和密码)在我的谷歌应用程序脚本中,以及基于文档的所有必需功能

-编辑-根据要求提供这些功能的完整代码

/**
 * 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);
}

和 list 文件

{
  "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 帐户的提示 connect with a google account

但我需要一种方法来为第三方服务提供凭据。 如果我使用我的 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);
  };

关于google-apps-script - Google Data Studio 连接器在第三方进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55430799/

相关文章:

google-apps-script - 不支持 SpreadsheetApp.getUi() 操作?

google-apps-script - Google App 脚本 LDAP 访问控制

Google Data Studio 中的语句不起作用的情况

looker-studio - 在 Google Data Studio 中运行 Delta Issue

calculated-columns - Google Data Studio 中的总和/详细程度

javascript - Google Data Studio 自定义图表

javascript - Google 脚本转换数字的方式与 JavaScript 不同

google-apps-script - 如何在Excel单元格中插入链接

looker-studio - 数据工作室 - 比较不同列的数据

Javascript:如何检查 fct 是否正在运行,如果没有运行,则使其运行?