【问题标题】:Error when making API call to Google向 Google 进行 API 调用时出错
【发布时间】:2017-09-15 05:32:53
【问题描述】:

我在 Google Apps 脚本 https://github.com/googlesamples/apps-script-oauth2/blob/master/samples/Smartsheet.gs 中使用这个 OAuth 库

并尝试调用 Google Site Verification API,但是在执行此操作时出现以下错误-:

https://www.googleapis.com/siteVerification/v1/token 的请求失败,返回代码 500。截断的服务器响应:{"error":{"errors":[{"domain":"global","re​​ason":"backendError","message":" Backend Error"}],"code":500,"message":"Backend Error"}}(使用 muteHttpExceptions 选项检查完整响应)(第 14 行,文件“Service”)

function run() {
  var service = getiService();
  if (service.hasAccess()) {

    var url = 'https://www.googleapis.com/siteVerification/v1/token';

    var result = UrlFetchApp.fetch(url, {
      'headers': {
        'Authorization' : 'Bearer ' + service.getAccessToken()
      },
      'method': 'GET',

  "site": {
    "identifier": "somedomainname.com",
    "type": "INET_DOMAIN"
  },
  "verificationMethod": "DNS_TXT"

                                   });

 Logger.log(result)   

感谢您的帮助。 谢谢

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    在我的环境中,the OAuth library that you say 无法使用。所以我自己检索了访问令牌和刷新令牌,并确认该脚本有效。如果您的环境也无法使用该库,请检索访问令牌并刷新令牌。

    如果你想使用https://www.googleapis.com/siteVerification/v1/token的端点获取验证令牌,可以参考WebResource: getToken。根据参考,方法是“post”。在UrlFetchApp.fetch(),它将请求正文写入payload。当这些反映到脚本中时,脚本变成如下。

    修改后的脚本:

    var url = 'https://www.googleapis.com/siteVerification/v1/token';
    var requestBody = {
      site: {
        identifier: "somedomainname.com",
        type: "INET_DOMAIN",
      },
      verificationMethod: "DNS_TXT",
    };
    var result = UrlFetchApp.fetch(url, {
      headers: {'Authorization': 'Bearer ' + service.getAccessToken()}, // In my environment, service.getAccessToken() was changed to access token I retrieved.
      method: 'post',
      payload: JSON.stringify(requestBody),
      contentType: 'application/json',
      muteHttpExceptions: true,
    });
    

    结果:

    {
      "method": "DNS_TXT",
      "token": "google-site-verification=#####"
    }
    

    【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2018-03-10
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2016-10-27
    • 2021-09-30
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多