在角度应用程序中,我需要请求yahoo contact API:首先,我想知道它是否可以从客户端请求?我用角表示的代码是:
ngOnInit(): void {
this.route.queryParams.subscribe(q => {
const body = {
client_id: this.yahooObj.clientId,
client_secret: this.yahooObj.clientSecret,
redirect_uri: this.yahooObj.redirect,
code: q.code,
grant_type: 'authorization_code'
}
this.http.post<any>('https://api.login.yahoo.com/oauth2/get_token',
body, {headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Authorization': 'Basic' + btoa(`${this.yahooObj.clientId}:${this.yahooObj.clientSecret}`)
})})
.subscribe(data => {
console.log('return data service', data);
}, (err: HttpErrorResponse) => {
console.log('Error', err);
});
});
}
yahooAuth() {
window.open(`https://api.login.yahoo.com/oauth2/request_auth?client_id=${this.yahooObj.clientId}&response_type=code&redirect_uri=${this.yahooObj.redirect}`);
}从雅虎身份验证页面重定向和beck代码运行良好,但是http.post请求返回到有关CORS策略的错误下面,因为在postman中工作很好。
CORS策略阻止了从源'‘访问的策略:对飞行前请求的响应不通过访问控制检查:请求的资源上不存在“访问控制-允许-原产地”报头。
因为在postman中,并且浏览器中有这个CORS策略错误,我需要确保有任何方法在客户端调用这个API?
发布于 2020-06-16 08:10:59
在Basic和令牌之间缺少一个空格。尝试以下几点
'Authorization': 'Basic ' + btoa(`${this.yahooObj.clientId}:${this.yahooObj.clientSecret}`)注意单词Basic后面的空格。
关于CORS问题,您可以在前端设置一个代理来重定向请求。
发布于 2020-06-16 08:12:45
您可以在后端服务器中编写Yahoo调用,并从角客户机调用API,这就是所谓的代理。这样才能解决CORS问题。
https://stackoverflow.com/questions/62403768
复制相似问题