-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2_auth.py
33 lines (26 loc) · 1.08 KB
/
oauth2_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from requests_oauthlib import OAuth2Session
from dotenv import load_dotenv
import os
load_dotenv()
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
authorization_base_url = os.getenv('AUTHORIZATION_URL')
token_url = os.getenv('TOKEN_URL')
redirect_uri = os.getenv('REDIRECT_URL')
# Function to automate OAuth flow
def get_access_token():
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)
authorization_url, state = oauth.authorization_url(authorization_base_url)
print(f"Go to: {authorization_url}")
redirect_response = input("Paste the full redirect URL after approval: ")
token = oauth.fetch_token(
token_url, authorization_response=redirect_response, client_secret=client_secret
)
return token
# Testing get user details using OAuth2 session token generated
def fetch_protected_resource(token):
oauth = OAuth2Session(client_id, token=token)
response = oauth.get("https://api.github.com/user")
return response.json()
user_info = fetch_protected_resource(get_access_token())
print(f"User Info: {user_info}")