节点获取发送发布请求,正文为x-www-form-urlencoded

问题描述

我想使用node-fetch发送一个带有x-www-form编码的主体有效负载的发布请求。我尝试了这段代码,但不幸的是它不起作用:
paypalSignIn = function(){
var username = process.env.PAYPALID;
var password = process.env.PAYPALSECRET;
var authContent = 'Basic '+base64.encode(username + ":" + password);

fetch('https://api.sandBox.paypal.com/v1/oauth2/token',{ method: 'POST',headers: {
       'Accept': 'application/json','Accept-Language' :"en_US",'Authorization': authContent,'Content-Type': 'application/x-www-form-urlencoded'

  },body: 'grant_type=client_credentials' })
    .then(res => res.json()) // expecting a json response
    .then(json => console.log(json));

} 我不确定这种方式是否可行,但我需要将该标准用于Paypal API。 我收到状态码400的错误

grant_type为空

Thx

解决方法

我不知道这是否是唯一的错误,但是至少在单词Basic和编码的用户名/密码之间需要一个空格。

下次您问一个问题时,张贴您的脚本返回的内容。我猜在这种情况下是401错误。