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

是否将以下Paypal curl命令转换为axios?,curl,vue.js,paypal,httprequest,axios,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” 我得到这个错误: Error i

如何将此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”

我得到这个错误:

 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错误):

相关文件:

CORS:

VueAxios:

贝宝开发:

根据:

根据报告:


这是一条很旧的线。我在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.

这是一条很旧的线。我在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.

没有关于axios的线索,但我很确定你混合了头和负载。是的。我不知道如何告知axios什么是头,什么是用户凭据,什么是数据…:/没有关于axios的线索,但我很确定你混合了头和负载。是的。我不知道如何告知axios什么是头,什么是用户凭据,什么是数据…:/谢谢,但这不起作用,我得到这个415错误:后415(不支持的媒体类型)。。。因此,我添加了content-type头来尝试修复它:“content-type”:“application/x-www-form-urlencoded”,现在它返回400 errorHmm。您可以尝试将凭据为true的
添加到
this.axios({})中吗
?我这样做了,并抛出了一个CORS错误:
访问位于的XMLHttpRequest'https://api.sandbox.paypal.com/v1/oauth2/token“起源”http://localhost:8080'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:响应中的'access control Allow Origin'标头的值不得为当请求的凭据模式为“包括”时,使用通配符“*”。XMLHttpRequest启动的请求的凭据模式由withCredentials属性控制。
ok。不确定您的服务器是否允许跨源请求,但请尝试将
{'Access Control Allow origin':'Authorization'}
添加到标题中。我也不确定
PayPal
API是否允许您直接从浏览器发出请求。如果没有,那么您需要设置一个服务器来代表您的前端发出请求;也就是说,设置一个服务器来发出这些请求,然后将结果发送到前端;谢谢,但这不起作用,我得到这个415错误:后415(不支持的媒体类型)。。。因此,我添加了content-type头来尝试修复它:“content-type”:“application/x-www-form-urlencoded”,现在它返回400 errorHmm。您可以尝试将凭据为true的
添加到
this.axios({})中吗
?我这样做了,并抛出了一个CORS错误:
访问位于的XMLHttpRequest'https://api.sandbox.paypal.com/v1/oauth2/token“起源”http://localhost:8080'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:响应中的'access control Allow Origin'标头的值不得为当请求的凭据模式为“包括”时,使用通配符“*”。XMLHttpRequest启动的请求的凭据模式由withCredentials属性控制。
ok。不确定您的服务器是否允许跨源请求,但请尝试将
{'Access Control Allow origin':'Authorization'}
添加到标题中。我也不确定
PayPal
API是否允许您直接从浏览器发出请求。如果没有,那么您需要设置一个服务器来代表您的前端发出请求;也就是说,设置一个服务器来发出这些请求,然后将结果发送到前端;
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'
    }
})
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
  }
})
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.