【问题标题】:Converting the following Paypal curl command to axios?将以下 Paypal curl 命令转换为 axios?
【发布时间】:2019-04-22 01:24:07
【问题描述】:

如何将此 paypal curl 命令转换为 Axios?

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"

我在 vue 中使用 vueAxios,因此是“this.”。

this.axios.post('https://api.sandbox.paypal.com/v1/oauth2/token',
    {'Accept': "application/json", 'Accept-Language': "en_US", 
    'XXXXXX': 'XXXXX', // my paypal client ID and client secret 
    'grant_type':'client_credentials'}
    )
    .then( response => {
        console.log("Response is: ", response);
    })
    .catch( error => {
        console.log("Error is :", error);
});

我收到此错误:

 Error is : Error: Request failed with status code 401
    at createError (createError.js?16d0:16)
    at settle (settle.js?db52:18)
    at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)

我也试过这个(这似乎更好,但我仍然收到 400 错误):

 this.axios({ 
    method: 'POST', 
    url: 'https://api.sandbox.paypal.com/v1/oauth2/token', 
    headers: {
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'Content-Type':'application/x-www-form-urlencoded'
    },
    auth: {
            username: '<XXXX My paypal client ID>',
            password: '<XXXX My paypal secret>'
    } ,
    data: {
        grant_type: 'client_credentials'
    },
})
.then(function(response) {console.log(response);})
.catch(function(response) {console.log(response);});

更新 – 在对 cme​​ts 进行一些帮助后,我尝试了以下代码,但 paypal 出现了 CORS 错误问题(我已经安装了一个 npm 包“cors”并且 cors 错误仍然存​​在(本地和部署时))。

这回答了我的问题,但是,as stated here,Paypal 似乎不允许直接来自浏览器的请求。

this.axios({
    withCredentials: true,
    url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
    method: 'post',
    headers: { 
        'Accept': 'application/json', 
        'Accept-Language': 'en_US',
        'Content-Type':'application/x-www-form-urlencoded',
        'Access-Control-Allow-Origin': '*',
        },
    data: { 'grant_type':'client_credentials' },
    auth: {
        username: 'XXXXXXXX',
        password: 'XXXXXXXX'
    }
})

相关文档:

CORS:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

VueAxios:https://www.npmjs.com/package/vue-axios

贝宝开发者:https://developer.paypal.com/docs/api/overview/#make-your-first-call

【问题讨论】:

  • 没有关于 axios 的线索,但我很确定你混合了标头和有效负载。
  • 是的。我不知道如何告知 axios 什么是 --header, --User 凭据是什么以及 --data 是什么...:/

标签: curl vue.js paypal httprequest axios


【解决方案1】:

这是一个相当古老的线程。我在 node.js 上也为此苦苦挣扎。 我以后的解决方案。希望这会有所帮助:

const res = await axios(
    {
        method: 'post',
        url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
        data: 'grant_type=client_credentials', // => this is mandatory x-www-form-urlencoded. DO NOT USE json format for this
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/x-www-form-urlencoded',// => needed to handle data parameter
            'Accept-Language': 'en_US',
        },
        auth: {
            username: clientId,
            password: clientSecret
        },

    });

console.log(JSON.stringify(res.data)); // => be sure to handle res.data, otherwise you will have a circular json error. The token you need is res.data.access_token.

【讨论】:

    【解决方案2】:

    根据axios GitHub docs

    this.axios({
      url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
      method: 'post',
      headers: { 
           'Accept': 'application/json', 
           'Accept-Language': 'en_US',
           'Content-Type':'application/x-www-form-urlencoded',
           'Access-Control-Allow-Origin': '*', 
      },
      data: { 'grant_type':'client_credentials' },
      auth: {
        username: client_id,
        password: secret
      }
    })
    

    【讨论】:

    • 谢谢,但这不起作用我得到这个 415 错误:POST api.sandbox.paypal.com/v1/oauth2/token 415(不支持的媒体类型)......所以我添加了内容类型标题来尝试修复它:'Content-Type':'application/x-www-form-urlencoded' 现在返回 400 错误
    • 嗯。您可以尝试将withCredentials: true 添加到this.axios({}) 吗?
    • 我做了,但引发了 CORS 错误:Access to XMLHttpRequest at 'https://api.sandbox.paypal.com/v1/oauth2/token' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
    • 好的。不确定您的服务器是否允许跨源请求,但请尝试将 { 'Access-Control-Allow-Origin': 'Authorization' } 添加到标头。
    • 我也不确定PayPal API 是否允许您直接从浏览器发出请求。如果没有,那么您需要设置一个服务器来代表您的前端发出请求;也就是说,设置一个服务器来发出这些请求,然后将结果发送到您的前端;
    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 2017-05-25
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多