2017-01-09 44 views
1

我想调用Twitters API并获取我的鸣叫,所以我可以将它们发布到我创建的网站上。Axios API Twitter请求不会返回用户鸣叫

当我运行下面的代码时,出现错误。

XMLHttpRequest cannot load https://api.twitter.com/1.1/search/tweets.json?q=%SamSchaeferSays . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3333 ' is therefore not allowed access. The response had HTTP status code 400." And "bundle.js:30041 Uncaught (in promise) Error: Network Error.

我新的API调用不使用PHP - 不知道我在做什么错在这里。

const tweet = 'https://api.twitter.com/1.1/search/tweets.json?q=%SamSchaeferSays'; 
function getUserTweet() { 
    return axios.get(`${tweet}`).then(function(response){ 
     console.log(response.data) 
     console.log(response.status) 
    }); 
} 

样品的OAuth串

const DST = `OAuth oauth_consumer_key="${consumerKey}", 
oauth_nonce="${}", 
oauth_signature="${}", 
oauth_signature_method="${}", 
oauth_timestamp="${}", 
oauth_token="${}", 
oauth_version="1.0" 
`; 
+0

'400'表示请求不好。入口点在语法上不正确。 –

回答

-1

一个400 Bad Request错误意味着服务器不明白你的要求。在你的情况下,有一个错误,阻止请求通过(额外%)。试试这个:

const tweet = 'https://api.twitter.com/1.1/search/tweets.json?q=SamSchaeferSays'; 
function getUserTweet() { 
    return axios.get(`${tweet}`, { headers: { 'Authorization': 'YOUR_OAUTH_HEADER' } }).then(function(response){ 
     console.log(response.data) 
     console.log(response.status) 
    }); 
} 

这将解决400 Bad Request错误,但你不会得到任何数据回来呢。 Twitter API要求您授权您的请求。了解更多their documentation

To allow applications to provide this information, Twitter’s API relies on the OAuth 1.0a protocol. At a very simplified level, Twitter’s implementation requires that requests needing authorization contain an additional HTTP Authorization header with enough information to answer the questions listed above. A version of the HTTP request shown above, modified to include this header, looks like this (normally the Authorization header would need to be on one line, but has been wrapped for legibility here):

+0

我以为OAuth是用于流式传输请求?我设置了一个,但授权标题让我感到困惑 - 就像我想从那里发出的那样。 – user3622460

+0

在我的axions.get中,我要发送我当前的信息+一个名为Authorization的数组,其中包含所有OAuth内容? – user3622460

+0

您需要在每次请求时设置此标头,Twitter对此非常严格。我用'Authorization'标题更新了我的问题。 –