2017-01-09 17 views
1

Twitters APIを呼び出して自分のツイートを取得しようとしていますので、私が作成しているウェブサイトに投稿することができます。Axios API

次のコードを実行するとエラーが発生します。

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.

私はPHPを使用しないAPI呼び出しには新しく、ここで間違っていることはわかりません。

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がストリーミング要求であると思ったのですか?私は1つを設定しましたが、Authorizationヘッダーが私を混乱させます - 私がそれを送信する場所と同じように。 – user3622460

+0

私のaxions.getで私は私の現在の情報+すべてのOAuthのものを含むAuthorizationという配列を送りますか? – user3622460

+0

リクエストごとにこのヘッダーを設定する必要があります。Twitterはこれに関して非常に厳しいです。質問を「Authorization」ヘッダで更新しました。 –

関連する問題