Search code examples
djangotwittertwitter-anywhere

Migrating from twitter anywhere api


I m using twitter anywhere api for allowing user to sign in with twitter and get their twitter data to store it my table.Since twitter anywhere api is going to expire soon how can i migrate this functionality to oauth.

my javascript:

<script src="https://platform.twitter.com/anywhere.js?id={{twitterappid}}&v=1"></script>
<script type="text/javascript">
var twt_connected = 0;
var Uuid = '2334443224';
$(function(){
if ($('#twtlogin').length) {
  // do something
twttr.anywhere(function(twitter) {
if(twitter.isConnected()){
//alert('Welcome, you are connected');
currentUser = twitter.currentUser;
screenName = currentUser.data('screen_name');
jQuery.ajax({url:"/twitter/Uuid="+Uuid+"/" ,
             type: "POST",
             data: {user: JSON.stringify(currentUser) },
             dataType: "json",
             success: function(result) {
                        }});
document.getElementById("twtlogin").innerHTML = '<img src="/images/icon_tconnected.gif" width="153" height="37" alt="Connected" />';
 }
 });
 }
 });

$("#login").click(function(e){
    e.preventDefault();
    if (twt_connected == 0){
    $.post("/twt-click-time/Uuid="+Uuid+"/","clicked",function(data){})
    twttr.anywhere(function (T) {
             T.bind("authComplete", function (e, user) {
            document.getElementById("twtlogin").innerHTML = '<img src="/images/icon_tconnected.gif" width="153" height="37" alt="Connected" />';
             twt_connected = 1;
             currentUser = T.currentUser;
             screenName = currentUser.data('screen_name');
             jQuery.ajax({url:"/twitter/Uuid="+Uuid+"/" ,
                         type: "POST",
                         data: {user: JSON.stringify(currentUser) },
                         dataType: "json",
                         success: function(result) {
                        }});

             });
             T.signIn();
             });
             }
             });

</script>

I m using django at my backend.


Solution

  • I'm doing this right now for flask. The easiest option is just to plug-in some server side oauth calls.

    It's a reasonably large amount of code so I won't copy and paste the whole thing here, but the github page for simplegeo's oauth2 actually has a "logging into django with twitter" walkthrough that should help out.

    After having gone through a few options, I think I like twython best. It's just this to do the first step of the oauth:

    from twython import Twython
    
    t = Twython(app_key='key',
                app_secret='secret',
                callback_url='http://google.com/')
    auth_props = t.get_authentication_tokens()
    print auth_props