angular - 如何在 Angular 后调用中正确发送 application/x-www-form-urlencoded

标签 angular paypal angular-httpclient

我正在尝试获取 Paypal 访问 token ,根据 paypal,请求的数据是 -d "grant_type=client_credentials" 并且必须在 application/x-www- 中发送它form-urlencoded 格式,下面是我现在正在尝试的代码

let body = "grant_type=client_credentials"

return this.httpClient.post<any (`https://api.sandbox.paypal.com/v1/oauth2/token`,body);

我正在身份验证拦截器中设置 header ,如下所示

const copiedReq = req.clone({ 
    setHeaders: {
      'Authorization': `Basic  ${this.client_id}:${this.secrete}`, 
      'Accept': 'application/json',
      'Content-Type':'application/x-www-form-urlencoded',
    }
});
return next.handle(copiedReq); 

它总是返回 401 Unauthorized,但我的 client_idsecret 没问题,因为当我尝试与 postman 通话时,它工作正常

网络选项卡

The network tab 它在具有相同凭据的 postman 上工作正常,但在这里不行,我做错了什么?我如何提出正确的请求以获得结果

Paypal curl 示例

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"

最佳答案

迟到总比不到好。我刚刚遇到了和你一样的问题,我终于解决了。 您必须对您的凭据进行 Base 64 编码:

let credentials: btoa(`${this.client_id}:${this.secrete}`);

const copiedReq = req.clone({ 
setHeaders: {
  'Authorization': `Basic  `+credentials, 
  'Accept': 'application/json',
  'Content-Type':'application/x-www-form-urlencoded',
}
});
return next.handle(copiedReq); 

几分钟前这对我有用。希望下次对您有用:D

关于angular - 如何在 Angular 后调用中正确发送 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57716741/

相关文章:

javascript - Angular Universal,在单击 'refresh' 两次之前应用程序不会加载

java - Paypal Rest API SDK v2

android - Android 是否支持通过 PayPal 进行应用内支付?

php - Angular 6 HttpClient 和 PHP 错误 (unknown url) : 0 Unknown Error 的 Http 失败响应

angular - 如何从 Angular http 的响应中读取状态?

angular - ViewEncapsulation.ShadowDom 与 ViewEncapsulation.Native

angular - RXJS - 对属性数组的多个可观察调用

javascript - Angular 2 : How to access an HTTP response body?

javascript - Angular 7,没有 NgModel 如何获取复选框状态(真/假)?

paypal - IPN + 结帐后将客户带到 URL