3

Tumblr provides a very simple interface to link your Twitter and Facebook accounts for posting through their service. I'd like to do something similar in my application - provide a single point for people to aggregate different accounts like Flickr, Facebook, Twitter, etc. - and I don't want to spend $1,000 per year for Janrain's Account Mapping to do so.

How do I link multiple accounts in aggregate using web2py? I have a feeling I should start here, but was hoping there were more concrete tutorials or best practices documented.

Matt Norris
  • 8,596
  • 14
  • 59
  • 90

1 Answers1

1

here what I'm doing in order to use my twitter account to login to my application.

First of all you need to sign for a twitter application here and get https://dev.twitter.com/ and the application key, application token and so forth. Then edit in your web2py application the db.py file and make sure you have the following:

## create all tables needed by auth if not custom tables
auth_table = db.define_table(
    auth.settings.table_user_name,
    Field('first_name', length=128, default=""),
    Field('last_name', length=128, default=""),
    Field('username', length=128, default="", unique=True),
    Field('password', 'password', length=256,
          readable=False, label='Password'),
    Field('registration_id', length=128, default= "",
          writable=False, readable=False))

auth_table.username.requires = IS_NOT_IN_DB(db, auth_table.username)

auth.define_tables()

at the bottom of the same file add:

#  Twitter API 
consumer_key = <your key>
consumer_secret =  <your secret>

request_token_url = 'https://twitter.com/oauth/request_token'
access_token_url = 'https://twitter.com/oauth/access_token'
authorize_url = 'https://twitter.com/oauth/authorize'

import gluon.contrib.simplejson as json

class TwitterOAuth(OAuthAccount):
    def get_user(self):        
        if self.accessToken() is not None:            
            client = Client(self.consumer, self.accessToken())
            resp, content = client.request('http://api.twitter.com/1/account/verify_credentials.json')
            if resp['status'] != '200':
                # cannot get user info. should check status
                return None
            u = json.loads(content)            
            return dict(username=u['screen_name'], name=u['name'], registration_id=str(u['id']))



auth.settings.login_form=TwitterOAuth(globals(),consumer_key,consumer_secret, 
authorize_url, request_token_url, access_token_url)

That's all.

Everything worked fine to me

Cheers