|
| 1 | +# Copyright 2019 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Get user credentials from interactive code environments. |
| 16 | +
|
| 17 | +This module contains helpers for getting user credentials from interactive |
| 18 | +code environments installed on a development machine, such as Jupyter |
| 19 | +notebooks. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import absolute_import |
| 23 | + |
| 24 | +import google_auth_oauthlib.flow |
| 25 | + |
| 26 | + |
| 27 | +def get_user_credentials(scopes, client_id, client_secret): |
| 28 | + """Gets credentials associated with your Google user account. |
| 29 | +
|
| 30 | + This function authenticates using your user credentials by going through |
| 31 | + the OAuth 2.0 flow. You'll open a browser window to authenticate to your |
| 32 | + Google account. The permissions it requests correspond to the scopes |
| 33 | + you've provided. |
| 34 | +
|
| 35 | + To obtain the ``client_id`` and ``client_secret``, create an **OAuth |
| 36 | + client ID** with application type **Other** from the `Credentials page on |
| 37 | + the Google Developer's Console |
| 38 | + <https://console.developers.google.com/apis/credentials>`_. Learn more |
| 39 | + with the `Authenticating as an end user |
| 40 | + <https://cloud.google.com/docs/authentication/end-user>`_ guide. |
| 41 | +
|
| 42 | + Args: |
| 43 | + scopes (Sequence[str]): |
| 44 | + A list of scopes to use when authenticating to Google APIs. See |
| 45 | + the `list of OAuth 2.0 scopes for Google APIs |
| 46 | + <https://developers.google.com/identity/protocols/googlescopes>`_. |
| 47 | + client_id (str): |
| 48 | + A string that identifies your application to Google APIs. Find |
| 49 | + this value in the `Credentials page on the Google Developer's |
| 50 | + Console |
| 51 | + <https://console.developers.google.com/apis/credentials>`_. |
| 52 | + client_secret (str): |
| 53 | + A string that verifies your application to Google APIs. Find this |
| 54 | + value in the `Credentials page on the Google Developer's Console |
| 55 | + <https://console.developers.google.com/apis/credentials>`_. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + google.oauth2.credentials.Credentials: |
| 59 | + The OAuth 2.0 credentials for the user. |
| 60 | +
|
| 61 | + Examples: |
| 62 | + Get credentials for your user account and use them to run a query |
| 63 | + with BigQuery:: |
| 64 | +
|
| 65 | + import google_auth_oauthlib |
| 66 | +
|
| 67 | + # TODO: Create a client ID for your project. |
| 68 | + client_id = "YOUR-CLIENT-ID.apps.googleusercontent.com" |
| 69 | + client_secret = "abc_ThIsIsAsEcReT" |
| 70 | +
|
| 71 | + # TODO: Choose the needed scopes for your applications. |
| 72 | + scopes = ["https://www.googleapis.com/auth/cloud-platform"] |
| 73 | +
|
| 74 | + credentials = google_auth_oauthlib.get_user_credentials( |
| 75 | + scopes, client_id, client_secret |
| 76 | + ) |
| 77 | +
|
| 78 | + # 1. Open the link. |
| 79 | + # 2. Authorize the application to have access to your account. |
| 80 | + # 3. Copy and paste the authorization code to the prompt. |
| 81 | +
|
| 82 | + # Use the credentials to construct a client for Google APIs. |
| 83 | + from google.cloud import bigquery |
| 84 | +
|
| 85 | + bigquery_client = bigquery.Client( |
| 86 | + credentials=credentials, project="your-project-id" |
| 87 | + ) |
| 88 | + print(list(bigquery_client.query("SELECT 1").result())) |
| 89 | + """ |
| 90 | + |
| 91 | + client_config = { |
| 92 | + "installed": { |
| 93 | + "client_id": client_id, |
| 94 | + "client_secret": client_secret, |
| 95 | + "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob"], |
| 96 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 97 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + app_flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_config( |
| 102 | + client_config, scopes=scopes |
| 103 | + ) |
| 104 | + |
| 105 | + return app_flow.run_console() |
0 commit comments