简体   繁体   中英

How to view/redirect to google plus user profile using django-social-auth?

Redirects to G+ profiles require "https://plus.google.com/<\\profile_id>/posts" format. My problem is that I haven't found the solution on how to get the profile_id dynamically for users of the site. It is not fetched by my current implementation of the django-social-auth (not saved in databases), and the UID field for google-oauth2 accounts records save the google email, maybe if I can find an implementation of how to redirect to a G+ profile using email, it would be fine (though some sources say it is not currently possible).

There is no way to use the Google+ API to determine the profile ID of a user from their email address.

However, if you're able to send the user through an OAuth 2.0 flow, you can determine their profile ID using the REST API's people get method . If you're using Google's API Python client library , the code would look something like this:

import apiclient.discovery
import httplib2

flow = OAuth2WebServerFlow(
    client_id=client_id,
    client_secret=client_secret,
    scope='https://www.googleapis.com/auth/plus.me',
    user_agent='google-api-client-python-plus-cmdline/1.0',
    xoauth_displayname='Google Plus Client Example App'
    )
credentials_file = 'plus_auth.dat'
storage = Storage(credentials_file)
credentials = run(flow, storage)

http = credentials.authorize(http)
service = apiclient.discovery.build('plus', 'v1', http=http, developerKey=api_key)
people_resource = service.people()    
people_document = people_resource.get(userId='me').execute()

print "ID: " + people_document['id']

You can also find a more complete example in the Google+ Python starter app

The user id is part of the data sent by google in the OAuth2 workflow, here's how you can store it and then access it:

  1. Define this setting:

    GOOGLE_OAUTH2_EXTRA_DATA = [ ('id', 'id') ]

  2. Access the id:

    social = user.social_auth.get(provider='google-oauth2')

    profile_id = social.extra_data['id']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM