【问题标题】:POST Request in Body as x-www-form-urlencodedPOST 请求正文为 x-www-form-urlencoded
【发布时间】:2021-06-30 13:46:40
【问题描述】:

我使用以下代码将grant_typeclient_idclient_secretrefresh token 发布到https://webexapis.com/v1/access_token 下面是我在 POstman 中尝试时成功的邮递员 Javascript-fetch 控制台

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");

var urlencoded = new URLSearchParams();
urlencoded.append("grant_type", "refresh_token");
urlencoded.append("client_id", "xxxx");
urlencoded.append("client_secret", "yyyy");
urlencoded.append("refresh_token", "zzzz");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: urlencoded,
  redirect: 'follow'
};

fetch("https://webexapis.com/v1/access_token", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

请帮助我在 Appscript 中获得结果。 所以我在 Google Sheets 脚本中尝试了以下代码,但它显示错误

function webexDev() {
    
var cisurl = "https://webexapis.com/v1/access_token";

var data = {
  'grant_type': 'refresh_token',
  'client_id': 'xxxx',
  'client_secret': 'yyyy',
  'refresh_token': 'zzzz'
};

var options = { 'method': 'post', 'payload': data, 'contentType': 'application/x-www-form-urlencoded'}

    var response = UrlFetchApp.fetch(cisurl,options);
    var cisjson=response.getContentText();
    var cisdata=JSON.parse(cisjson)
   Logger.log(cisjson)
}

我在 Postman 中尝试了同样的方法,并且成功了。我想在 Google Sheet Script 中做同样的事情

【问题讨论】:

  • 当您点击“发送”时,请查看 Postman 生成的实际请求,而不是(或同时)查看 Postman 中的用户界面。然后您将看到屏幕截图中的数据如何转换为有效请求。使用 that 作为您需要在脚本中构建的指南..
  • 另外:"x-www-form-urlencoder" 似乎有一个错字(它以d 而不是r 结尾。它是内容类型的一部分(参见here)。它也是表单数据的 default 内容类型。请参阅 Postman 中的“标题”选项卡。

标签: javascript google-apps-script http-headers


【解决方案1】:

你把事情搞混了。这些参数 grant_type、client_id、client_secret 和 refresh_token 不是 HTTP 标头,而是您发送到服务器的有效负载。

根据文档 (https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app),这应该有效:

var cisurl = "https://webexapis.com/v1/access_token";

var data = {
  'grant_type': 'refresh_token',
  'client_id': 'abcdefg',
  'client_secret': 'hijklmn',
  'refresh_token': 'opqrstuvw'
};

var options = {
  'method': 'post',
  'payload': data
}

var response = UrlFetchApp.fetch(cisurl,options);

P.S.:不要在 stack-overflow 上分享你的秘密

【讨论】:

  • function webexDevice() { var cisurl = "https://webexapis.com/v1/access_token"; var data = { 'grant_type': 'refresh_token', 'client_id': 'xxxx', 'client_secret': 'yyyy', 'refresh_token': 'S00MjVm' }; var options = { 'method': 'post', 'payload': data } var response = UrlFetchApp.fetch(cisurl,options); var cisjson=response.getContentText(); var cisdata=JSON.parse(cisjson) Logger.log(cisjson)
  • 我收到错误Exception: Request failed for https://webexapis.com returned code 400. Truncated server response: {"message":"POST failed: HTTP/1.1 400 Bad Request (url = https://idbroker.webex.com/idb/oauth2/v1/access_token, request/response TrackingId = ROUTE... (use muteHttpExceptions option to examine full response)
  • 好吧,也许您必须明确设置 contentType:您可以在选项中使用 "contentType": 'application/x-www-form-urlencoded' 试试吗?
  • 显示错误 var options = { 'method': 'post', 'payload': data 'application/x-www-form-urlencoded'} 对吗
  • 这是什么错误?它在语法上应该是正确的?
猜你喜欢
  • 2021-01-19
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 2015-06-18
相关资源
最近更新 更多