【问题标题】:How to emulate a curl request using node-fetch module如何使用 node-fetch 模块模拟 curl 请求
【发布时间】:2019-06-22 13:39:33
【问题描述】:

我有一个电子商务应用程序,并试图联系 paypal rest api,特别是“paypal for partners”服务,我确实阅读了Paypal Documentation,这一切都很好,但问题是他们提到了请求示例像这样使用卷曲:

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
   -H "Accept: application/json" \
   -H "Accept-Language: en_US" \
   -u "client_id:secret" \
   -d "grant_type=client_credentials"

或者

使用带有基本身份验证的邮递员:

  • 用户名:您的客户 ID。

  • 密码:你的秘密。

我试图实现相同的东西,但使用 node.js 中的 node-fetch

const fetch = require('node-fetch');

function authenticatePaypal() {
    fetch('https://api.sandbox.paypal.com/v1/oauth2/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'client_id': 'secret'
        },
        body: {
            "grant_type": "client_credentials"
        }
    }).then(reply => {
        console.log('success');
        console.log(reply);
    }).catch(err => {
        console.log('error');
        console.log(err);
    });
}

module.exports = {
    authenticatePaypal: authenticatePaypal
};

我收到了 401 Unauthorized 的回复:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
     status: 401,
     statusText: 'Unauthorized',
     headers: Headers { [Symbol(map)]: [Object] } } }

我试过邮递员,它在邮递员中工作,我知道我的节点获取实现有问题,这是我第一次处理 json 格式的基本身份验证。

【问题讨论】:

  • 我确定问题出在我的 headers 对象中我不知道如何以 json 格式实现基本 Auth
  • 尝试删除此标头 'Content-Type': 'application/json,我不太确定,但请尝试确认这是否可能是原因
  • @MahmoudFawzy 客户端 ID 似乎是基本身份验证,因此应该类似于 Authorization: Basic <secret in base64>
  • @MahmoudFawzy 应该是Authorization: Basic <client_id:secret in base64>
  • @MahmoudFawzy 您确定要以 JSON 格式发送吗?我认为应该只是一个字符串body: 'grant_type=client_credentials'

标签: javascript node.js curl paypal paypal-sandbox


【解决方案1】:

授权标头错误。

-u "client_id:secret"

说 curl 正在使用Basic Authentication

您应该添加授权标头

Authorization: Basic <base64 encoded "client_id:secret">

【讨论】:

  • 我得到了 415 Unsupported Media Type 但我解决了它购买更改 'Content-Type': 'application/x-www-form-urlencoded' 并且正文看起来像那个字符串 body: "grant_type=client_credentials" 感谢您的帮助
【解决方案2】:

使用你的作为基础的解决方案,因为我努力了几分钟才能让它工作。

// get the client_id and secret from https://developer.paypal.com/developer/applications/
const clientIdAndSecret = <client_id:secret>
const base64 = Buffer.from(clientIdAndSecret).toString('base64')

fetch('https://api.sandbox.paypal.com/v1/oauth2/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept-Language': 'en_US',
        'Accept': 'application/json',
        'Authorization': `Basic ${base64}`,
      },
      body: 'grant_type=client_credentials'
})

【讨论】:

    猜你喜欢
    • 2016-11-27
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2022-01-24
    • 2017-10-01
    相关资源
    最近更新 更多