how to change facebook oauth2 redirect_uri

136 views
Skip to first unread message

killzane

unread,
Apr 10, 2016, 3:38:49 AM4/10/16
to web2py-users
I followed the web2py book http://web2py.com/books/default/chapter/29/09/access-control#Other-login-methods-and-login-forms and put the example code in my model

and I use this in my controllers named default.py
def fbLogin():
auth.settings.login_form=FaceBookAccount()
form = auth()

return dict(form=form)

When I visit this page, I can see facebook Oauth.
there are some problem I want to solve.
1. why the redirect_uri set to [app]/default/user/login but not [app]/default/fbLogin?
may I set it manual?

2. facebook suggest me to show a window to show the Oauth page, but I'm not really know how to do it in web2py.
I should use ajax to do it? or some other way?

Michele Comitini

unread,
Apr 10, 2016, 3:48:18 PM4/10/16
to web...@googlegroups.com
You should be able to change redirect url using the standard settings of auth.

http://www.web2py.com/books/default/chapter/29/09/access-control#Auth-Settings-and-messages

You do not need ajax, just a window.open() or a <a href=".." target="_blank"> or an iframe



--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

killzane

unread,
Apr 11, 2016, 1:21:10 AM4/11/16
to web2py-users
I see all settings but I don't know what attribute I should change.
the redirect_uri args I mean is when log with facebook will goto 

client_id=568390199919780&
scope=email,user_birthday&

because if the rediect_uri don't have the FaceBookAccount() instance it will show error and can't login success.

So I just use a "_blank" link to open my fbLogin page?

mcm於 2016年4月11日星期一 UTC+8上午3時48分18秒寫道:

Michele Comitini

unread,
Apr 11, 2016, 4:55:58 AM4/11/16
to web...@googlegroups.com
In the standard web2py authentication is triggered by @auth.requires_login decorator if the user is not authenticated already.

in model after defining a class FacebookAccount() extended from gluon.contrib.oauth20_account.OAuthAccount


auth.settings.login_form = FacebookAccount()

# change default login action
# default is auth.settings.login_url = URL('user', args='login')
auth.settings.login_url = URL(c='mycontroller', f='my_login_action', args=['login'])

in mycontroller.py:

def my_login_action():
  """same as default/login action"""
  return dict(form=auth())


@auth.requires_login():
def f():
  ...

killzane

unread,
Apr 11, 2016, 6:33:51 AM4/11/16
to web...@googlegroups.com
Hi

I have tried your suggestion and put the code
auth.settings.login_url = URL(c='default', f='fbLogin', args=['login'])

this is in my default.py
def fbLogin():
auth.settings.login_form=FaceBookAccount()
auth.settings.login_url = URL(c='default', f='fbLogin', args=['login'])
form = auth()

return dict(form=form)

No matter I put in db.py or my controller,
when user login will see like the picture


the url is
?state=auth_provider%3Dfacebook
&redirect_uri=https%3A%2F%2Fmydomain%2F[app]%2Fdefault%2Fuser%2Flogin
&response_type=code
&client_id=MY_CLIENT_ID
&scope=public_profile%2C+email
&display=popup

the redirect_uri is what I want to change. as above code. redirect_uri = https://mydomain/[app]/default/user/login
And I want it change to  https://mydomain/[app]/default/fbLogin.



mcm於 2016年4月11日星期一 UTC+8下午4時55分58秒寫道:
Reply all
Reply to author
Forward
0 new messages