将以下Paypal curl命令转换为axios?

时间:2018-11-19 16:21:41

标签: curl vue.js paypal httprequest axios

如何将此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);});

更新-在获得一些帮助后,我尝试了以下代码,并且贝宝(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

Paypal开发人员:https://developer.paypal.com/docs/api/overview/#make-your-first-call

2 个答案:

答案 0 :(得分: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
  }
})

答案 1 :(得分:2)

这是一个相当古老的线程。我也在用 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.