2015-11-21 14 views
10

私は、ソーシャルサイトをウェブサイトに統合するのが非常に新しいです。私は多少Facebookを統合することができましたが、私はTwitterをどのように統合するのか分かりません。CodebirdでのTwitter認証JS

Twitterアカウントからログインし、ユーザー名やその他のデータをTwitterから取得したいと考えています。私は消費者キーと消費者の秘密を持っています。私はここから進める方法がわかりません。私のGoogleの検索はこれまで役に立っていません。

私はcodebird JSにしようとしています:

$(function() { 
    $('#twitter').click(function(e) { 
     e.preventDefault(); 
     var cb = new Codebird; 
     cb.setConsumerKey("redacted", "redacted"); 
     cb.__call(
      "oauth_requestToken", 
      { oauth_callback: "http://127.0.0.1:49479/" }, 
      function (reply, rate, err) { 
       if (err) { 
        console.log("error response or timeout exceeded" + err.error); 
       } 
       if (reply) { 
        // stores it 
        cb.setToken(reply.oauth_token, reply.oauth_token_secret); 

        // gets the authorize screen URL 
        cb.__call(
         "oauth_authorize", 
         {}, 
         function (auth_url) { 
          window.codebird_auth = window.open(auth_url); 
         } 
        ); 
       } 
      } 
     ); 
     cb.__call(
      "account_verifyCredentials", 
      {}, 
      function(reply) { 
       console.log(reply); 
      } 
     );     
    }) 
}); 

しかし、私は

あなたの資格情報がどのように私はこの問題を解決して取得することができ、このリソースへのアクセス

ことはできません取得ユーザーデータ?私は別のTwitterの実装を使用することにオープンしています。

+0

のOAuthコールバックを作ったのですか?それとも、ここのコードコピーのためだけですか? –

+0

localhostはうまくいきます - Twitterは楽しく任意のURIにリダイレクトし、OPにはおそらくdevページが設定されています。 –

+1

あなたのTwitter APIコンシューマーキーと秘密を投稿から削除し、https://apps.twitter.com/で再生成してください。彼らは秘密にすることを意図しています。 – Jublo

答えて

3

cb._call("account_verifyCredentials"...に電話をかけることはできません。

コードにはアクセストークンではなく、ユーザーがあなたのアプリを承認した後にのみ受信する(トークン認証ポップアップで)リクエストトークンしかありません。

documented on the README.のように「PINなしのコールバックURL」メソッドを使用しているため、http://127.0.0.1:49479/ページにそのサンプルコードを実装する必要があります。

また、これには基本的に、oauth資格情報をどこかに格納する必要があります。私の例では、localStorageを使用しました。どのように努め -

$(function() { 
    $('#twitter').click(function (e) { 
    e.preventDefault(); 
    var cb = new Codebird; 
    cb.setConsumerKey("CeDhZjVa0d8W02gWuflPWQmmo", "YO4RI2UoinJ95sonHGnxtYt4XFtlAhIEyt89oJ8ZajClOyZhka"); 

    var oauth_token = localStorage.getItem("oauth_token"); 
    var oauth_token_secret = localStorage.getItem("oauth_token_secret"); 
    if (oauth_token && oauth_token_secret) { 
     cb.setToken(oauth_token, oauth_token_secret); 
    } else { 
     cb.__call(
     "oauth_requestToken", { 
      oauth_callback: "http://127.0.0.1:49479/" 
     }, 
     function (reply, rate, err) { 
      if (err) { 
      console.log("error response or timeout exceeded" + err.error); 
      } 
      if (reply) { 
      console.log("reply", reply) 
       // stores it 
      cb.setToken(reply.oauth_token, reply.oauth_token_secret); 

      // save the token for the redirect (after user authorizes) 
      // we'll want to compare these values 
      localStorage.setItem("oauth_token", reply.oauth_token); 
      localStorage.setItem("oauth_token_secret", reply.oauth_token_secret); 

      // gets the authorize screen URL 
      cb.__call(
       "oauth_authorize", {}, 
       function (auth_url) { 
       console.log("auth_url", auth_url); 
       // JSFiddle doesn't open windows: 
       // window.open(auth_url); 
       $("#authorize").attr("href", auth_url); 

       // after user authorizes, user will be redirected to 
       // http://127.0.0.1:49479/?oauth_token=[some_token]&oauth_verifier=[some_verifier] 
       // then follow this section for coding that page: 
       // https://github.com/jublonet/codebird-js#authenticating-using-a-callback-url-without-pin 
       }); 
      } 
     }); 
    } 
    }) 
}); 

はまた、ローカルホスト上のJSFiddle

+0

私はいつも401を手に入れました –

関連する問題