Joa*_*cho 3 curl paypal httprequest vue.js axios
如何将此 paypal curl 命令转换为 Axios?
\n\ncurl -v https://api.sandbox.paypal.com/v1/oauth2/token \\\n-H "Accept: application/json" \\\n-H "Accept-Language: en_US" \\\n-u "client_id:secret" \\\n-d "grant_type=client_credentials"\nRun Code Online (Sandbox Code Playgroud)\n\n我在 vue 中使用 vueAxios,因此是“ this. ”。
\n\nthis.axios.post(\'https://api.sandbox.paypal.com/v1/oauth2/token\',\n {\'Accept\': "application/json", \'Accept-Language\': "en_US", \n \'XXXXXX\': \'XXXXX\', // my paypal client ID and client secret \n \'grant_type\':\'client_credentials\'}\n )\n .then( response => {\n console.log("Response is: ", response);\n })\n .catch( error => {\n console.log("Error is :", error);\n});\nRun Code Online (Sandbox Code Playgroud)\n\n我收到此错误:
\n\n Error is : Error: Request failed with status code 401\n at createError (createError.js?16d0:16)\n at settle (settle.js?db52:18)\n at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)\nRun Code Online (Sandbox Code Playgroud)\n\n我也尝试过这个(这似乎更好,但我仍然收到 400 错误):
\n\n this.axios({ \n method: \'POST\', \n url: \'https://api.sandbox.paypal.com/v1/oauth2/token\', \n headers: {\n \'Accept\': \'application/json\',\n \'Accept-Language\': \'en_US\',\n \'Content-Type\':\'application/x-www-form-urlencoded\'\n },\n auth: {\n username: \'<XXXX My paypal client ID>\',\n password: \'<XXXX My paypal secret>\'\n } ,\n data: {\n grant_type: \'client_credentials\'\n },\n})\n.then(function(response) {console.log(response);})\n.catch(function(response) {console.log(response);});\nRun Code Online (Sandbox Code Playgroud)\n\n在一些帮助形成评论后更新 \xe2\x80\x93 我尝试了以下代码,并且 paypal 有问题 CORS 错误(我已经安装了 npm 包“cors”并且 cors 错误仍然存在(本地和部署时)) 。
\n\n这回答了我的问题,但是,正如此处所述,Paypal 似乎不允许直接来自浏览器的请求。
\n\nthis.axios({\n withCredentials: true,\n url: \'https://api.sandbox.paypal.com/v1/oauth2/token\',\n method: \'post\',\n headers: { \n \'Accept\': \'application/json\', \n \'Accept-Language\': \'en_US\',\n \'Content-Type\':\'application/x-www-form-urlencoded\',\n \'Access-Control-Allow-Origin\': \'*\',\n },\n data: { \'grant_type\':\'client_credentials\' },\n auth: {\n username: \'XXXXXXXX\',\n password: \'XXXXXXXX\'\n }\n})\nRun Code Online (Sandbox Code Playgroud)\n\n相关文档:
\n\nCORS: https: //developer.mozilla.org/en-US/docs/Web/HTTP/CORS
\n\nVueAxios: https://www.npmjs.com/package/vue-axios
\n\nPaypal 开发:https://developer.paypal.com/docs/api/overview/#make-your-first-call
\n小智 9
这是一个相当古老的线程。我在使用 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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2397 次 |
| 最近记录: |