我正在尝试获取 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_id
和 secret
没问题,因为当我尝试与 postman 通话时,它工作正常
网络选项卡
它在具有相同凭据的 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/