【问题标题】:Google Apps Script : API Error message "client_id is required"Google Apps 脚本:API 错误消息“client_id is required”
【发布时间】:2021-09-15 13:24:58
【问题描述】:

我创建了一个变量来保存客户端 ID (CLIENT_ID),但我不断收到一条错误消息,指出运行此函数时需要客户端 ID。有人知道我在这里做错了什么吗?

function getAuth() {
  var authBasedUrl = 'https://test-api.service.hmrc.gov.uk/oauth/authorize';
  var response = UrlFetchApp.fetch(authBasedUrl,  {
      headers: {
        'Accept' : 'application/vnd.hmrc.1.0+json',
        'response_type': 'code',
        'client_id' : CLIENT_ID,
        'scope' : 'hello',
        'redirect_uri' : 'https://www.xxxxxxx.com/'
      }});
  var result = JSON.parse(response.getContentText());
  Logger.log(JSON.stringify(result, null, 2));
  } 

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    根据docs,您需要发出 POST 请求。页面上有一个blockqoute说:

    在请求正文中包含请求参数,而不是作为请求 标题。

    编辑:

    function getAuth() {
      var authBasedUrl = 'https://test-api.service.hmrc.gov.uk/oauth/token';
      var options = {
        headers: {
          'Accept': 'application/vnd.hmrc.1.0+json',
          "Content-Type": "application/json"
        },
        payload: JSON.stringify({
          client_id: 'RgwU6hvdxxxxxxic6LwIt',
          client_secret: '9e8c9yyyyyyyyyyc2fc2ed9126',
          grant_type: 'client_credentials',
          scope: 'hello'
        })
      }
      var response = UrlFetchApp.fetch(authBasedUrl, options);
      var result = JSON.parse(response.getContentText());
    
      console.log(result);
    }
    

    【讨论】:

    • 谢谢,我尝试运行您的代码,但是出现了另一条错误消息:“异常:test-api.service.hmrc.gov.uk 的请求失败,返回代码 400。截断的服务器响应:{” error":"invalid_request","error_description":"client_id is required"} (使用 muteHttpExceptions 选项检查完整响应)" 知道为什么会出现问题吗?我确实设置了客户端 ID 和 Secret
    • 我已经更新了代码。当我测试它时,我得到了一个令牌。我错过了内容类型
    • 我已经用我的客户 ID 和秘密运行了代码,它的工作原理,谢谢!
    • 请标记答案。 @h-edwards
    猜你喜欢
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2019-05-16
    相关资源
    最近更新 更多