You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LinkedIn API OAUTH授权返回grant_type参数缺失错误如何解决

错误原因

你将OAuth认证所需的必填参数放在了请求头(headers)中,不符合LinkedIn客户端凭证流的接口要求:grant_type、client_id、client_secret 属于POST请求体参数,需要放在请求体中传递,接口无法从请求头中读取到这些参数,因此抛出grant_type缺失的错误。

修正方案

将参数从headers中移除,放到UrlFetchApp请求配置的payload字段中,Google App Script会自动按照你指定的application/x-www-form-urlencoded格式对payload内容进行编码后发送。

修正后的完整代码
function callLinkedAPI () {
  // 认证参数放在payload中,而非headers
  var payload = {
    "grant_type": "client_credentials",
    "client_id": "78ciob33iuqepo",
    "client_secret": "deCgAOhZaCrvweLs"
  }

  var url = `https://www.linkedin.com/oauth/v2/accessToken/`

  var requestOptions = {
    'method': "POST",
    "headers": {},
    'payload': payload,
    'contentType': 'application/x-www-form-urlencoded',
    'muteHttpExceptions': true
  };

  var response = UrlFetchApp.fetch(url, requestOptions);
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  console.log(json)
}

注意:生产环境不要将client_id、client_secret等敏感信息硬编码在代码中,建议使用Google App Script的「脚本属性」功能存储敏感凭证,避免泄露。

内容的提问来源于stack exchange,提问作者Mario Perry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:48:05