我尝试使用仅应用程序身份验证调用 Twitter 搜索 API
但我无法按照文档获得不记名 token :https://dev.twitter.com/oauth/application-only
let urlAuth = 'https://api.twitter.com/oauth2/token';
let apiKey = encodeURIComponent('xxx');
let apiKeySecret = encodeURIComponent('xxx');
let encoded = btoa(apiKey + ':' + apiKeySecret);
let body = 'grant_type=client_credentials';
let options = {
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'Authorization': 'Basic ' + encoded
})
};
this.http.post(urlAuth, body, options)
.subscribe(data => {
console.log('twitter auth', data);
});
这是网络日志:
Request URL:https://api.twitter.com/oauth2/token
Request Method:OPTIONS
Status Code:400
Remote Address:104.244.42.194:443
是否可以从本地主机获取?来自客户?
最佳答案
这是因为 CORS 预检请求 ( https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests )
现在 Twitter API 似乎不支持 CORS。唯一的方法是从后端服务器发出请求。
关于javascript - Angular 2 - Twitter 搜索 api - 预检时仅应用程序身份验证错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40135346/