Web2py Integration with Multiple Oauth Logins

262 views
Skip to first unread message

James Q

unread,
Feb 18, 2014, 6:33:29 PM2/18/14
to web...@googlegroups.com
I have a feeling that this topic comes up often, but I cannot seem to Google a good solution to this. Essentially, I want users to be able to login using either the built in Auth, or login via some Oauth provider, like Twitter and Facebook. I have seen previous solutions using ExtendedLoginForm and custom Twitter classes that inherit from OAuthProvider, but they no longer seem to work. The Twitter code I find no longer supports the newest Twitter API; the linkedin code from the web2py book also  does not work.

Here is sample code I have now, which only seems to constantly redirect me to the logout page:

1) Created new web2py application by copying the welcome one to a folder named 'oauth'
2) Added this code to db.py (taken from multiple examples online):

from gluon.contrib.login_methods.oauth10a_account import OAuthAccount
from oauth2 import Client, Consumer, Token

class TwitterAccount(OAuthAccount):
    AUTH_URL = "http://twitter.com/oauth/authorize"
    TOKEN_URL = "https://twitter.com/oauth/request_token"
    ACCESS_TOKEN_URL = "http://twitter.com/oauth/access_token"
    CLIENT_ID = "..."
    CLIENT_SECRET = "..."

    def __init__(self, g):
        OAuthAccount.__init__(self, g, self.CLIENT_ID, self.CLIENT_SECRET, self.AUTH_URL, self.TOKEN_URL, self.ACCESS_TOKEN_URL)

    def get_user(self):
        if self.accessToken() is not None:
            consumer = Consumer(key=self.CLIENT_ID, secret=self.CLIENT_SECRET)
            client = Client(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
                #redirect("http://google.com")
                return None
            u = json.loads(content)
            return dict(first_name = u['name'], username=u['screen_name'], name=u['name'], registration_id=u['id'])

auth.settings.login_form=TwitterAccount(g=globals())

3) Going to 127.0.0.1/oauth/user/login redirects me to user/logout.


Besides my frustration in trying to find timely information about this, is there working code out there that is *recent*, in that it works with latest web2py and the latest Oauth implementations? I'd really appreciate any tips or hints anyone has.


Quint

unread,
Feb 21, 2014, 3:55:00 AM2/21/14
to web...@googlegroups.com
Hi,
 
You could have a look at python-social-auth and if you like it and don't know how to integrate it, have a look at this plugin to integrate it in web2py.
 
 
Quint

James Q

unread,
Feb 21, 2014, 10:15:11 PM2/21/14
to web...@googlegroups.com
Interesting plugin, thanks for the link.

I took that repository, tar'ed and gzip'ed it, and uploaded it as a plugin. Says "App does not exist or you are not authorized". No idea what that means, so I did it manually in a new app called oauth. Here is what I get as an exception when trying to open up the new app:

Traceback (most recent call last):
File "/home/james/Development/web2py/gluon/restricted.py", line 217, in restricted
exec ccode in environment
File "/home/james/Development/deemok-web2py/applications/oauth/models/plugin_social_auth.py", line 7, in <module>
Field('user', 'reference auth_user', writable=False, notnull=True))
File "/home/james/Development/web2py/gluon/dal.py", line 8139, in define_table
table = self.lazy_define_table(tablename,*fields,**args)
File "/home/james/Development/web2py/gluon/dal.py", line 8156, in lazy_define_table
table = table_class(self, tablename, *fields, **args)
File "/home/james/Development/web2py/gluon/dal.py", line 8665, in __init__
check_reserved(field_name) File "/home/james/Development/web2py/gluon/dal.py", line 7850, in check_reserved_keyword
'invalid table/column name "%s" is a "%s" reserved SQL/NOSQL keyword' % (name, backend.upper()))
SyntaxError: invalid table/column name "uid" is a "ALL" reserved SQL/NOSQL keyword

Can you possibly rename the field:

Field('uid', 'string', notnull=True, writable=False, length=255),

to:

Field('oauth_uid', 'string', notnull=True, writable=False, length=255),

Or something along those lines?

Quint

unread,
Feb 22, 2014, 3:41:00 AM2/22/14
to web...@googlegroups.com
Hi,

About the exception:

I changed the Field name to "oauth_uid".
I did not notice this problem because i'm using GAE.

About "App does not exist or you are not authorized":

This means that your oauth key/secret is not properly set in your configuration.
You are not supplying a valid/authorized oauth client key to the provider.
Which provider is giving this message?
What does your configuration look like?

What is find strange is that that plugin packed/installed by yourself is giving different result than te one installed manually. This should be exactly the same right? Are you sure everything is included in the one packed by yourself?

Regards,

Quint

James Q

unread,
Feb 23, 2014, 4:21:05 PM2/23/14
to web...@googlegroups.com
Hello again,

Thanks for taking your time to engage with me. Hopefully I can get this working sometime soon.

Regarding the update, I re-cloned the repository and notied a w2p file there. I used this file to upload to the web2py admin interface, which gives me the same error as before. Are you saying that I need to extract it first, add my keys to it, and then try uploading it?

Either way, I did it manually using tar and I get this traceback now, which is similar to the above:

Traceback (most recent call last):
File "/home/james/Development/web2py/gluon/restricted.py", line 217, in restricted
exec ccode in environment
Field('user', 'reference auth_user', writable=False, notnull=True))
File "/home/james/Development/web2py/gluon/dal.py", line 8139, in define_table
table = self.lazy_define_table(tablename,*fields,**args)
File "/home/james/Development/web2py/gluon/dal.py", line 8156, in lazy_define_table
table = table_class(self, tablename, *fields, **args)
File "/home/james/Development/web2py/gluon/dal.py", line 8665, in __init__
check_reserved(field_name)
File "/home/james/Development/web2py/gluon/dal.py", line 7850, in check_reserved_keyword
'invalid table/column name "%s" is a "%s" reserved SQL/NOSQL keyword' % (name, backend.upper()))
SyntaxError: invalid table/column name "user" is a "ALL" reserved SQL/NOSQL keyword

Guess that needs to be changed as well :)

-- James


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/5JkLj4muEbw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Quint

unread,
Feb 24, 2014, 9:32:29 AM2/24/14
to web...@googlegroups.com
Hi there,

I changed that field as well. (and another field)

about the w2p:

Don't know whats going on there. But I tarred it myself using 7zip but apparently that doesn't work. I cannot package or upload using the admin interface on my GAE dev server, gives internal error.

Could someone give me the steps to properly package a w2p without using the admin interface?

(I removed it from the repo)

Quint

Quint van den Muijsenberg

unread,
Feb 24, 2014, 1:28:55 PM2/24/14
to web...@googlegroups.com

James,

May I suggest we continue this discussion in the google code repo? You could create an issue with a link to this thread.

Quint

Reply all
Reply to author
Forward
0 new messages