|
18 | 18 | import os |
19 | 19 | import subprocess |
20 | 20 |
|
| 21 | +import six |
| 22 | + |
21 | 23 | from google.auth import environment_vars |
22 | | -import google.oauth2.credentials |
| 24 | +from google.auth import exceptions |
23 | 25 |
|
24 | 26 |
|
25 | 27 | # The ~/.config subdirectory containing gcloud credentials. |
|
34 | 36 | _CLOUD_SDK_WINDOWS_COMMAND = "gcloud.cmd" |
35 | 37 | # The command to get the Cloud SDK configuration |
36 | 38 | _CLOUD_SDK_CONFIG_COMMAND = ("config", "config-helper", "--format", "json") |
| 39 | +# The command to get google user access token |
| 40 | +_CLOUD_SDK_USER_ACCESS_TOKEN_COMMAND = ("auth", "print-access-token") |
37 | 41 | # Cloud SDK's application-default client ID |
38 | 42 | CLOUD_SDK_CLIENT_ID = ( |
39 | 43 | "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com" |
@@ -80,21 +84,6 @@ def get_application_default_credentials_path(): |
80 | 84 | return os.path.join(config_path, _CREDENTIALS_FILENAME) |
81 | 85 |
|
82 | 86 |
|
83 | | -def load_authorized_user_credentials(info): |
84 | | - """Loads an authorized user credential. |
85 | | -
|
86 | | - Args: |
87 | | - info (Mapping[str, str]): The loaded file's data. |
88 | | -
|
89 | | - Returns: |
90 | | - google.oauth2.credentials.Credentials: The constructed credentials. |
91 | | -
|
92 | | - Raises: |
93 | | - ValueError: if the info is in the wrong format or missing data. |
94 | | - """ |
95 | | - return google.oauth2.credentials.Credentials.from_authorized_user_info(info) |
96 | | - |
97 | | - |
98 | 87 | def get_project_id(): |
99 | 88 | """Gets the project ID from the Cloud SDK. |
100 | 89 |
|
@@ -122,3 +111,42 @@ def get_project_id(): |
122 | 111 | return configuration["configuration"]["properties"]["core"]["project"] |
123 | 112 | except KeyError: |
124 | 113 | return None |
| 114 | + |
| 115 | + |
| 116 | +def get_auth_access_token(account=None): |
| 117 | + """Load user access token with the ``gcloud auth print-access-token`` command. |
| 118 | +
|
| 119 | + Args: |
| 120 | + account (Optional[str]): Account to get the access token for. If not |
| 121 | + specified, the current active account will be used. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + str: The user access token. |
| 125 | +
|
| 126 | + Raises: |
| 127 | + google.auth.exceptions.UserAccessTokenError: if failed to get access |
| 128 | + token from gcloud. |
| 129 | + """ |
| 130 | + if os.name == "nt": |
| 131 | + command = _CLOUD_SDK_WINDOWS_COMMAND |
| 132 | + else: |
| 133 | + command = _CLOUD_SDK_POSIX_COMMAND |
| 134 | + |
| 135 | + try: |
| 136 | + if account: |
| 137 | + command = ( |
| 138 | + (command,) |
| 139 | + + _CLOUD_SDK_USER_ACCESS_TOKEN_COMMAND |
| 140 | + + ("--account=" + account,) |
| 141 | + ) |
| 142 | + else: |
| 143 | + command = (command,) + _CLOUD_SDK_USER_ACCESS_TOKEN_COMMAND |
| 144 | + |
| 145 | + access_token = subprocess.check_output(command, stderr=subprocess.STDOUT) |
| 146 | + # remove the trailing "\n" |
| 147 | + return access_token.decode("utf-8").strip() |
| 148 | + except (subprocess.CalledProcessError, OSError, IOError) as caught_exc: |
| 149 | + new_exc = exceptions.UserAccessTokenError( |
| 150 | + "Failed to obtain access token", caught_exc |
| 151 | + ) |
| 152 | + six.raise_from(new_exc, caught_exc) |
0 commit comments