Madusudhanan
Madusudhanan

Reputation: 349

Google drive access from django-python

In Django I implemented social login using the social auth app and I followed this link to configure it. Goolge OAuth is working good; google-oauth2 access token is stored in extra data field.

Now I want to list google drive files using this access token. I tried with this.

def drive(request):
    user = request.user
    social = user.social_auth.get(provider='google-oauth2')
    response = requests.get(
        'https://www.googleapis.com/auth/drive.metadata.readonly',
        params={'access_token': social.extra_data['access_token']})
    print(response)
    return render(request, 'home/drive.html', {'checking':response})

I am getting a 200 response, but I don't know how to list files.

I'm using django 2.0.3 and python 3.5.

Upvotes: 1

Views: 3608

Answers (1)

Madusudhanan
Madusudhanan

Reputation: 349

Change the settings to Re-prompt Google OAuth2 users to refresh the refresh_token by

SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = { 'access_type': 'offline' }

and used a google-auth library to authenticate to Google APIs

def drive(request):
    user = request.user
    social = user.social_auth.get(provider='google-oauth2')
    creds=google.oauth2.credentials.Credentials(social.extra_data['access_token'])
    drive = googleapiclient.discovery.build('drive', 'v3', credentials=creds)
    files = drive.files().list().execute()

References:

  1. https://developers.google.com/api-client-library/python/auth/web-app
  2. https://google-auth.readthedocs.io/en/latest/user-guide.html
  3. https://python-social-auth-docs.readthedocs.io/en/latest/use_cases.html

Upvotes: 1

Related Questions