2015-11-21 3 views
10

में ट्विटर प्रमाणीकरण मैं वेबसाइट पर सामाजिक साइटों को एकीकृत करने के लिए बहुत नया हूं। मैंने कुछ हद तक फेसबुक को एकीकृत करने में कामयाब रहा, लेकिन मुझे नहीं पता कि ट्विटर को कैसे एकीकृत किया जाए।कोडबर्ड जेएस

मैं एक ट्विटर खाते के माध्यम से लॉगिन करना चाहता हूं, फिर ट्विटर से उपयोगकर्ता नाम और कुछ अन्य डेटा प्राप्त करें। मेरे पास उपभोक्ता कुंजी और उपभोक्ता रहस्य है। मुझे यकीन नहीं है कि यहां से कैसे आगे बढ़ना है, और मेरी 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); 
      } 
     );     
    }) 
}); 

लेकिन मैं

आपके क्रेडेंशियल इस संसाधन

के लिए उपयोग की अनुमति नहीं है मैं यह कैसे हल करने और प्राप्त कर सकते हैं मिल उपयोगकर्ता का आधार - सामग्री? मैं एक वैकल्पिक ट्विटर कार्यान्वयन का उपयोग करने के लिए खुला हूँ।

+0

OAuth कॉलबैक बनाया? या यह केवल कोड कॉपी के लिए है? –

+0

लोकलहोस्ट ठीक है - ट्विटर खुशी से किसी भी यूआरआई पर रीडायरेक्ट करेगा, और ओपी में शायद एक देव पेज सेटअप होगा। –

+1

कृपया अपनी ट्विटर एपीआई उपभोक्ता कुंजी और पोस्ट से गुप्त हटाएं और उन्हें https://apps.twitter.com/ पर पुन: उत्पन्न करें। वे गुप्त रखा जाना है। – Jublo

उत्तर

3

आप cb._call("account_verifyCredentials"... पर कॉल नहीं कर सकते हैं।

कोड में केवल एक टोकन का अनुरोध है, एक एक्सेस टोकन नहीं है, जिसे उपयोगकर्ता केवल आपके ऐप (ट्विटर ऑथ पॉपअप पर) अधिकृत करने के बाद ही प्राप्त होगा।

आप "पिन के बिना कॉलबैक यूआरएल" विधि का उपयोग कर रहे हैं, documented on the README. के रूप में आपको अपने http://127.0.0.1:49479/ पृष्ठ पर उस उदाहरण कोड को लागू करने की आवश्यकता होगी।

इसके अलावा, यह अनिवार्य रूप से आवश्यक है कि आप कहीं भी ओथ क्रेडेंशियल स्टोर करें। नीचे दिए गए मेरे उदाहरण में, मैंने 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 मिल गया –