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

时间:2019-03-30 11:07:26

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

我正在建立与第三方来源Siteimprove的Google Data Studio连接器。 Siteimprove有一个需要Basic Access Authentication的api。

我已经在Google Apps脚本中设置了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);
}

清单文件

{
  "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响应,因此似乎可以正常工作。

有什么线索可以提示我提供第三方服务的凭据吗?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

Data Studio始终会首先根据脚本的作用域提示您进行Google授权,然后根据getAuthType()函数进行其他配置。由于您的getAuthType()函数为USER_TOKEN,因此在使用google授权后,您将获得一个提示,要求您使用这些凭据进行授权。

代码实验室的

The 4th step概述了连接器的流程,因此您可以查看何时调用什么函数。

您还希望确保至少定义了getAuthType()getData()getSchema()getConfig()。由于您使用的是USER_TOKEN身份验证类型,因此您必须按照Authentication

中的概述定义其他方法
相关问题