Reputation: 349
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
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:
Upvotes: 1