From 85ad6a7499b6205aedd3deda55b117bdee09a984 Mon Sep 17 00:00:00 2001 From: Mart van de Ven Date: Wed, 6 Mar 2019 23:20:37 +0800 Subject: [PATCH 1/4] interim commit --- src/flask_social_blueprint/providers.py | 42 +++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/flask_social_blueprint/providers.py b/src/flask_social_blueprint/providers.py index a1d8155..bb930d3 100644 --- a/src/flask_social_blueprint/providers.py +++ b/src/flask_social_blueprint/providers.py @@ -79,7 +79,7 @@ def __init__(self, *args, **kwargs): }, 'request_token_params': { 'response_type': 'code', - 'scope': 'https://www.googleapis.com/auth/plus.me email' + 'scope': 'profile' } } defaults.update(kwargs) @@ -90,6 +90,7 @@ def get_profile(self, raw_data): import oauth2client.client as googleoauth import apiclient.discovery as googleapi import httplib2 + from googleapiclient import discovery credentials = googleoauth.AccessTokenCredentials( access_token=access_token, @@ -97,23 +98,38 @@ def get_profile(self, raw_data): ) http = httplib2.Http() http = credentials.authorize(http) - api = googleapi.build('plus', 'v1', http=http) - profile = api.people().get(userId='me').execute() - name = profile.get('name') + service = discovery.build('people', 'v1', http=http) + + profile = service.people().get(resourceName='people/me', + personFields='names,emailAddresses,' + 'urls,photos') + + result = profile.execute() + + es = result['emailAddresses'] + ns = result['names'] + ps = result['photos'] + + primary_account = [e for e in es if 'primary' in e[u'metadata']][0] + primary_name = [n for n in ns if 'primary' in n[u'metadata']][0] + primary_photo = [p for p in ps if 'primary' in p[u'metadata']][0] + + profile_id = result['resourceName'].split('/')[-1] + data = { - 'provider': "Google", - 'profile_id': profile['id'], + 'provider': u"Google", + 'profile_id': profile_id, 'username': None, - "email": profile.get('emails')[0]["value"], + "email": primary_account.get('value'), 'access_token': access_token, 'secret': None, - "first_name": name.get("givenName"), - "last_name": name.get("familyName"), - 'cn': profile.get('displayName'), - 'profile_url': profile.get('url'), - 'image_url': profile.get('image', {}).get("url") + "first_name": primary_name.get("givenName"), + "last_name": primary_name.get("familyName"), + 'cn': primary_name.get('displayName'), + 'profile_url': None, + 'image_url': primary_photo.get('url') } - return ExternalProfile(str(profile['id']), data, raw_data) + return ExternalProfile(profile_id, data, raw_data) class Facebook(BaseProvider): From 2a409e6e57ad553f4c39e34ae3bbc5592eef8619 Mon Sep 17 00:00:00 2001 From: Mart van de Ven Date: Wed, 6 Mar 2019 23:27:57 +0800 Subject: [PATCH 2/4] profileId is a string --- src/flask_social_blueprint/providers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flask_social_blueprint/providers.py b/src/flask_social_blueprint/providers.py index bb930d3..0eec1c1 100644 --- a/src/flask_social_blueprint/providers.py +++ b/src/flask_social_blueprint/providers.py @@ -129,7 +129,7 @@ def get_profile(self, raw_data): 'profile_url': None, 'image_url': primary_photo.get('url') } - return ExternalProfile(profile_id, data, raw_data) + return ExternalProfile(str(profile_id), data, raw_data) class Facebook(BaseProvider): From 5ce8d85a8b184e35af69c8114821c5fc9aebda9a Mon Sep 17 00:00:00 2001 From: Mart van de Ven Date: Thu, 7 Mar 2019 00:17:45 +0800 Subject: [PATCH 3/4] Clean up --- src/flask_social_blueprint/providers.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/flask_social_blueprint/providers.py b/src/flask_social_blueprint/providers.py index 0eec1c1..ec12a1c 100644 --- a/src/flask_social_blueprint/providers.py +++ b/src/flask_social_blueprint/providers.py @@ -87,22 +87,24 @@ def __init__(self, *args, **kwargs): def get_profile(self, raw_data): access_token = raw_data['access_token'] - import oauth2client.client as googleoauth - import apiclient.discovery as googleapi + import httplib2 from googleapiclient import discovery + import oauth2client.client as googleoauth credentials = googleoauth.AccessTokenCredentials( access_token=access_token, user_agent='' ) + http = httplib2.Http() http = credentials.authorize(http) - service = discovery.build('people', 'v1', http=http) + resource_mask = 'names,emailAddresses,photos' + + service = discovery.build('people', 'v1', http=http) profile = service.people().get(resourceName='people/me', - personFields='names,emailAddresses,' - 'urls,photos') + personFields=resource_mask) result = profile.execute() @@ -110,14 +112,13 @@ def get_profile(self, raw_data): ns = result['names'] ps = result['photos'] + profile_id = result['resourceName'].split('/')[-1] primary_account = [e for e in es if 'primary' in e[u'metadata']][0] primary_name = [n for n in ns if 'primary' in n[u'metadata']][0] primary_photo = [p for p in ps if 'primary' in p[u'metadata']][0] - profile_id = result['resourceName'].split('/')[-1] - data = { - 'provider': u"Google", + 'provider': "Google", 'profile_id': profile_id, 'username': None, "email": primary_account.get('value'), From 1e6eed973ea055969fddca1a40f859fbfbde3d31 Mon Sep 17 00:00:00 2001 From: Mart van de Ven Date: Fri, 3 May 2019 14:39:13 +0630 Subject: [PATCH 4/4] Also request the userinfo.email auth scope --- src/flask_social_blueprint/providers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flask_social_blueprint/providers.py b/src/flask_social_blueprint/providers.py index ec12a1c..c44181a 100644 --- a/src/flask_social_blueprint/providers.py +++ b/src/flask_social_blueprint/providers.py @@ -79,7 +79,7 @@ def __init__(self, *args, **kwargs): }, 'request_token_params': { 'response_type': 'code', - 'scope': 'profile' + 'scope': 'profile email' } } defaults.update(kwargs)