Last active
September 14, 2023 04:03
-
-
Save terashim/226c291e499665b55692035e5448064b to your computer and use it in GitHub Desktop.
PythonでGoogleのデフォルト認証についての状態を検証する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""PythonでGoogleの認証についての状態を検証する | |
""" | |
import os | |
import google.auth | |
import google.auth.transport.requests | |
from google.auth.exceptions import DefaultCredentialsError | |
def get_tokeninfo(): | |
"""現在のデフォルト認証情報を使用してトークン情報を取得する | |
""" | |
credentials, _ = google.auth.default() | |
if isinstance(credentials, google.auth.credentials.Scoped): | |
credentials = credentials.with_scopes(['email']) | |
session = google.auth.transport.requests.AuthorizedSession(credentials) | |
response = session.get("https://www.googleapis.com/oauth2/v1/tokeninfo") | |
return response.text | |
if __name__ == "__main__": | |
try: | |
print("デフォルトの認証情報でトークン情報の取得を試みます...") | |
tokeninfo = get_tokeninfo() | |
print("トークン情報を取得しました:") | |
print(tokeninfo) | |
except DefaultCredentialsError as ex: | |
print("デフォルト認証情報を自動的に設定することができません") | |
raise ex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
google-auth | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment