【问题标题】:Google Data Studio connector authenticate at third partyGoogle Data Studio 连接器在第三方进行身份验证
【发布时间】:2019-03-30 11:07:26
【问题描述】:

我正在为第三方来源 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 响应,因此这似乎可以按预期工作。

我将如何获得提示为第三方服务提供凭据的任何线索?

【问题讨论】:

  • 我没有连接器配置页面? “编辑”菜单中没有“连接器”项。
  • 是的,这正是我正在做的。这将显示这个window。当我点击“授权”时,我会从原来的帖子中弹出窗口。
  • 你需要显示所有4个函数代码和清单文件。
  • 在原帖中查看我的编辑

标签: google-apps-script google-data-studio


【解决方案1】:
/**
* 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);
  };

【讨论】:

    【解决方案2】:

    Data Studio 将始终根据您的脚本范围首先提示 Google 授权,然后根据您的 getAuthType() 函数提示其他配置。由于您的getAuthType() 函数是USER_TOKEN,因此在通过 google 授权后,您将获得使用这些凭据进行授权的额外提示。

    codelab 的The 4th step 概述了连接器的流程,因此您可以看到何时调用了哪些函数。

    您还需要确保至少定义了getAuthType()getData()getSchema()getConfig()。由于您使用的是USER_TOKEN 的身份验证类型,因此您必须按照Authentication 中所述定义其他方法

    【讨论】:

    • 昨天我正在考虑这个问题并意识到这可能是这种情况。感谢您指出这一点,因为现在我知道要搜索的方向了!是的,我已经定义了这些功能。我会处理我的代码,如果我运行了,我会回来检查!
    • @JeroenSwets 你有没有运行过这个?我也有兴趣将 Siteimprove 连接到数据工作室,但应用脚本经验有限。
    • 是的,我做到了!伙计们,已经很久了,我会检查我的代码,看看我到底做了什么,然后回复你
    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 2017-04-29
    • 2018-08-21
    • 2019-11-26
    • 2012-05-19
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多