Gmail Oauth2 (Email ID and Access_Token)

80 views
Skip to first unread message

Great Avenger Singh

unread,
Jun 19, 2015, 9:27:16 AM6/19/15
to python-so...@googlegroups.com
Hello Everyone and PSA,

I am making an Gmail Analysis App using Python-Social-Auth. I am able to make Successful Login using Django Example provided in PSA Github Repository.

After the Successful Login it is returning  


{u'access_token': u'ya29.lwFayYRhgLMPnfdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
 
u'email': u'ars...@gmail.com',

 u'expires_in': 3598,

 u'family_name': u'Singh',

 u'gender': u'male',

 u'given_name': u'Great Avenger',

 u'id': u'112228104845783653330',


and other related information.


I want to access access_token and email


I tried following method:

 from social.backends.google import GoogleOAuth2
 
but there is no parameter access_token ?

I want access_token and User_Email for IMAP login, So I can able to access INBOX and do required Analysis.

Here is my IMAP Login code:

https://gist.github.com/arshpreetsingh/cd11e1854858dd3bb0d9

Bill Blanchard

unread,
Jun 19, 2015, 12:44:18 PM6/19/15
to python-so...@googlegroups.com
from social.apps.django_app.default.models import UserSocialAuth

instance = UserSocialAuth.objects.get(pk=XXXX)
access_token = instance.extra_data['access_token']
email = instance.email



Great Avenger Singh

unread,
Jul 7, 2015, 3:45:12 PM7/7/15
to python-so...@googlegroups.com


On Friday, 19 June 2015 22:14:18 UTC+5:30, Bill Blanchard wrote:
from social.apps.django_app.default.models import UserSocialAuth

instance = UserSocialAuth.objects.get(pk=XXXX)
access_token = instance.extra_data['access_token']
email = instance.email
 
access_token is working fine for me but when I use Email it shows me the following error:

'UserSocialAuth' object has no attribute 'email'

Great Avenger Singh

unread,
Jul 7, 2015, 5:13:20 PM7/7/15
to python-so...@googlegroups.com
On Wed, Jul 8, 2015 at 1:15 AM, Great Avenger Singh <ars...@gmail.com> wrote:

>> instance = UserSocialAuth.objects.get(pk=XXXX)
>> access_token = instance.extra_data['access_token']
>> email = instance.email
>
>
> access_token is working fine for me but when I use Email it shows me the
> following error:
>
> 'UserSocialAuth' object has no attribute 'email'

I used "uid" instead of email:

email = instance.uid

it is returning me email_id.

--

Thanks
Arshpreet Singh

I am Sikh boy, Learning by doing and Learning by teaching is my religion.
------------------------------------------------------------------------------
Slashdot TV.
Video for Nerds. Stuff that matters.
http://tv.slashdot.org/

Great Avenger Singh

unread,
Jul 19, 2015, 3:41:23 PM7/19/15
to python-so...@googlegroups.com
On Wed, Jul 8, 2015 at 2:43 AM, Great Avenger Singh <ars...@gmail.com> wrote:

>>> instance = UserSocialAuth.objects.get(pk=XXXX)

I am little bit confused here, If I am using pk = 1 it is returning
One email ID all the time, I want to access different Email IDs, Which
value of PK I should use?

Great Avenger Singh

unread,
Jul 21, 2015, 10:26:18 AM7/21/15
to python-so...@googlegroups.com
On Mon, Jul 20, 2015 at 1:11 AM, Great Avenger Singh <ars...@gmail.com> wrote:
> On Wed, Jul 8, 2015 at 2:43 AM, Great Avenger Singh <ars...@gmail.com> wrote:
>
>>>> instance = UserSocialAuth.objects.get(pk=XXXX)
>
> I am little bit confused here, If I am using pk = 1 it is returning
> One email ID all the time, I want to access different Email IDs, Which
> value of PK I should use?



Please help me to solve this issue.

Matías Aguirre

unread,
Jul 21, 2015, 11:09:04 AM7/21/15
to Great Avenger Singh, python-social-auth

Email is stored in the user instance, access_token in the UserSocialAuth instance. So, you can do this in a shell:

    user = User.objects.get(username=...)
    social = user.social_auth.get(provider="google-oauth2")
    email = user.email
    access_token = social.extra_data["access_token"]

Hope that helps,
Matías

Great Avenger Singh

unread,
Jul 28, 2015, 10:18:12 AM7/28/15
to Matías Aguirre, python-so...@googlegroups.com
On Tue, Jul 21, 2015 at 9:20 PM, Matías Aguirre <matias...@gmail.com> wrote:

> How you get the users is up to you and your project and automated
> procedure, my code was just an example. Basically what you need is:
>
> 1. Get a User instance
> 2. Get the associated social account
> 3. Get email from user
> 4. Get access_token from social account
I tried with all my head but could not understand what actually I need to get,

Do I need to creat Custom user model first (But I guess not, because I
just need Email and Acees token and PSA is already providing me that)

Can you give me any code example to play with at Django shell
?(including the libraries I need to import)

Here is default pipeline:

SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'example.app.pipeline.require_email',
'social.pipeline.mail.mail_validation',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.debug.debug',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'social.pipeline.debug.debug'
)

If in the last line social.pipeline.user.user_details which is
supposed to return user_details Can't get Email and access token from
that?

Thanks,
Reply all
Reply to author
Forward
0 new messages