将以下Paypal curl命令转换为axios? [英] Converting the following Paypal curl command to axios?

查看:96
本文介绍了将以下Paypal curl命令转换为axios?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I convert this paypal curl command to 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.".

I am using vueAxios in vue, hence the "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错误):

I've also tried this (which seems to be better but I'm still getting a 400 error):

 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错误持续存在(在本地和部署时)).

UPDATE – after some help form the comments I have tried the following code and the paypal has an issue CORS error ( i have installed a npm packages "cors" and the cors error persists (both locally and when deployed)).

这回答了我的问题,但是,如此处所述,看来贝宝没有不允许直接来自浏览器的请求.

This answers my question but, as stated here, it seems like Paypal doesn't allow requests directly from the browser.

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

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

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

Paypal dev: https://developer.paypal.com/docs/api/overview/#make-your-first-call

推荐答案

根据 axios GitHub文档:

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
  }
})

这篇关于将以下Paypal curl命令转换为axios?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆