javascript - 推特 :Application-only authentication error Origin null is not allowed by Access-Control-Allow-Origin

标签 javascript jquery twitter cors

我正在尝试向 Twitter 发送仅应用程序身份验证请求以获取 access_token。我编写了一个 javascript 代码

函数 getTwitterAuthorizeTokens() { 返回{oauth_token:“abcdefg”,oauth_token_secret:“asdfasdfasfdasdf”};

// Get the consumer key and secret
function getTwitterConsumerTokens() {
    return {key: "xxxxxxxxxxxxxxxxx", secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"};
}
var s = encodeURIComponent(getTwitterConsumerTokens().key,'RFC 1738');
s += ':'+encodeURIComponent(getTwitterConsumerTokens().secret,'RFC 1738');
$( document ).ready(function(){
$.ajax({
        type:"POST",
        beforeSend: function (request)
        {
            request.setRequestHeader("Authorization", "Basic "+s);
            request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        },
        url: "https://api.twitter.com/oauth2/token",
        data: "grant_type=client_credentials",
        processData: false,
        success: function(msg) {
            alert("successfull");
        }
});

加载此脚本时,它在控制台中给我错误

OPTIONS https://api.twitter.com/oauth2/token 405 (Method Not Allowed) sinon-server-1.7.1.js:573
apply sinon-server-1.7.1.js:573
fakeXhr.(anonymous function) sinon-server-1.7.1.js:592
send jquery.min.js:4
f.extend.ajax jquery.min.js:4
(anonymous function) index.html:143
e.resolveWith jquery.min.js:2
e.extend.ready jquery.min.js:2
c.addEventListener.C jquery.min.js:2
XMLHttpRequest cannot load https://api.twitter.com/oauth2/token. Origin null is not allowed by Access-Control-Allow-Origin. index.html:1

如何解决这个错误,请指导。我正在关注此链接https://dev.twitter.com/docs/auth/application-only-auth

当我使用 node.js 托管它时。它给我错误

Failed to load resource: the server responded with a status of 405 (Method Not Allowed) https://api.twitter.com/oauth2/token?jsonp=success
XMLHttpRequest cannot load https://api.twitter.com/oauth2/token?jsonp=success. Origin http://localhost:8888 is not allowed by Access-Control-Allow-Origin. index.html:1

最佳答案

您正在从您的浏览器发送跨域 ajax 请求,出于安全原因,这在大多数情况下是不允许的。您必须从您的服务器而不是客户端的浏览器发送请求。

关于javascript - 推特 :Application-only authentication error Origin null is not allowed by Access-Control-Allow-Origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17762057/

相关文章:

javascript - 在内部网站上访问外部图片 URL 时出现 403 错误

javascript - jQuery 不处理事件

javascript - 使用 jquery 更改 html <em> 标记内的文本

javascript - jQuery 在最后一个元素之后插入

Node.js 获取 Twitter Streaming API - EADDRNOTAVAIL

iphone - UIActionSheet 和 Twitter 用户名选择

javascript - 为 jqGrid 使用自定义 JSON 格式

javascript - 添加单选按钮的值

javascript - Facebook 分享按钮等于 Twitter 推文按钮

Javascript:全局变量不起作用。 (使用SDK.REST.retriveMultipleRecords)