diff --git a/src/flask_social_blueprint/providers.py b/src/flask_social_blueprint/providers.py index a1d8155..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': 'https://www.googleapis.com/auth/plus.me email' + 'scope': 'profile email' } } defaults.update(kwargs) @@ -87,33 +87,50 @@ 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) - api = googleapi.build('plus', 'v1', http=http) - profile = api.people().get(userId='me').execute() - name = profile.get('name') + + resource_mask = 'names,emailAddresses,photos' + + service = discovery.build('people', 'v1', http=http) + profile = service.people().get(resourceName='people/me', + personFields=resource_mask) + + result = profile.execute() + + es = result['emailAddresses'] + 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] + data = { 'provider': "Google", - 'profile_id': profile['id'], + '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(str(profile_id), data, raw_data) class Facebook(BaseProvider):