Last active 1748151650

DraxCodes's Avatar DraxCodes revised this gist 1748151650. Go to revision

1 file changed, 34 insertions

test.py(file created)

@@ -0,0 +1,34 @@
1 + import google.auth.transport.requests
2 + import google_auth_oauthlib.flow
3 +
4 + CLIENT_SECRETS_FILE = "client_secrets.json"
5 + SCOPES = ["openid", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"]
6 +
7 + def authenticate_user():
8 + flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
9 + CLIENT_SECRETS_FILE, SCOPES)
10 +
11 + credentials = flow.run_local_server(port=0)
12 +
13 + return credentials
14 +
15 + def main():
16 + credentials = authenticate_user()
17 +
18 + #Test print to see the request
19 + #print("OAuth Credentials:", json.dumps(credentials.to_json(), indent=2))
20 +
21 + user_info_url = "https://www.googleapis.com/oauth2/v2/userinfo"
22 + headers = {"Authorization": f"Bearer {credentials.token}"}
23 +
24 + response = google.auth.transport.requests.Request().session.get(user_info_url, headers=headers)
25 +
26 + if response.status_code == 200:
27 + user_info = response.json()
28 + print("Logged-in User's Google Name:", user_info.get("name"))
29 + print("Logged-in User's Google Email:", user_info.get("email"))
30 + else:
31 + print("Failed to retrieve user information.")
32 +
33 + if __name__ == "__main__":
34 + main()
Newer Older