I'm beginning with Oauth2 authentication with django allauth. I followed this tutorial The authentication with Google OAuth is working Now I'm wondering how to use this authenticated User to use another API that is using Google Oauth too by using information of the already logged in user. I imagine that the process is based on using the AccessToken used at first app authentication to log in the API, API that will validate this accessToken to Google too. But honestly I don't see at all the way to implement this behaviour Is there some explanation or tutorial or documentation describing this king of architecture ? or maybe I'm wrong on the way and there is a better way to do that ? Thank you in advance RV
Asked
Active
Viewed 178 times
1 Answers
0
Connect to other Google APP
To coonect to other Google solution, you can try to use google-auth.
1 Install google-auth
pip install google-auth
2 Get the token from the user logged in using Django-AllAuth with his Google Account. Django AllAuth stores the access token in the SocialToken model
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
access_token = request.user.socialaccount_set.get(provider='google').socialtoken_set.get().token
# Now you have the access token to use for API requests
3 Get token and make requests to the desired Google API using the access token. Here's an example using the Google Calendar API
from google.oauth2 import service_account
from googleapiclient.discovery import build
@login_required
def my_view(request):
access_token = request.user.socialaccount_set.get(provider='google').socialtoken_set.get().token
# Create a credentials object using the access token
credentials = service_account.Credentials.from_authorized_user_info(
{'access_token': access_token},
['https://www.googleapis.com/auth/calendar']
)
# Build the Google Calendar API service
service = build('calendar', 'v3', credentials=credentials)
# Now you can make requests to the Google Calendar API using the service object
# For example, list the user's calendars
calendars = service.calendarList().list().execute()
for calendar in calendars['items']:
print(calendar['summary'])
Some ressources:
- Here's the link of another post talking about the same solution
- Google Auth Python Library User Guide documentation for credential
- Link to Google auth page package
EDIT:
Sign In to other app using OAUTH2
To give a general overview of how to connect to another app, here's 2 example:
StackExchange
- you can find the steps to connect to StackExchange at this link.
- And some discussion about it at this post.

Philippe Sisowath
- 1,400
- 1
- 6
- 11
-
Hi thank you for answer It look simple but I can understand that it is for consuming a google API My need is more generic and I would to consume a custom API that is using Google Auth authentication too, but not a Google API Is this case working with your proposition ? or do i have to use a more generic API library compliant with OAuth2 to do this ? – Hervé LE BARS May 13 '23 at 13:23
-
OAuth2 is fine, though It will greatly depend of the app you want to connect to. Here's doc to [connect to StackExchange](https://api.stackexchange.com/docs/authentication) and a [post about it](https://stackoverflow.com/questions/36343556/how-to-use-oauth2-to-access-stackexchange-api). Or example for [Reddit](https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example#python-example) – Philippe Sisowath May 13 '23 at 19:24