javascript - Angular 2 - Twitter 搜索 api - 预检时仅应用程序身份验证错误 400

标签 javascript angular twitter cors

我尝试使用仅应用程序身份验证调用 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/

相关文章:

angular - Angular 数管道是否将数字转换为阿拉伯语?

python - Tweepy 上限为用户的 3182 条推文

r - twitteR 和 R 的 Twitter 表情符号编码问题

javascript - 如何在自定义工具提示中显示数据

Javascript 跨脚本变量值

javascript - Angular2 如何使用 Javascript XMLHttpRequest 重定向到另一个组件?

json - 如何验证 Twitter api 请求?

javascript - 在 IE11 中使用 CSS 动画化 SVG 路径

javascript - 使用谷歌脚本计算持续时间

javascript - 如果不存在,如何有效地将元素添加/推送到数组