Skip to content

Commit dd0a1a2

Browse files
tswastbusunkim96
authored andcommitted
Add get_user_credentials function to get your user credentials. (#40)
* Add `get_user_credentials` function to get your user credentials. This function is intended to be used in interactive code examples, such as from a Jupyter notebook or when developing scripts locally.
1 parent b6d8eed commit dd0a1a2

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

google_auth_oauthlib/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
"""oauthlib integration for Google Auth
16+
17+
This library provides `oauthlib <https://oauthlib.readthedocs.io/>`__
18+
integration with `google-auth <https://google-auth.readthedocs.io/>`__.
19+
"""
20+
21+
from .interactive import get_user_credentials
22+
23+
__all__ = ["get_user_credentials"]
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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()

tests/test_interactive.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import mock
16+
17+
18+
def test_get_user_credentials():
19+
from google_auth_oauthlib import flow
20+
from google_auth_oauthlib import interactive as module_under_test
21+
22+
mock_flow_instance = mock.create_autospec(
23+
flow.InstalledAppFlow,
24+
instance=True
25+
)
26+
27+
with mock.patch(
28+
"google_auth_oauthlib.flow.InstalledAppFlow", autospec=True
29+
) as mock_flow:
30+
mock_flow.from_client_config.return_value = mock_flow_instance
31+
module_under_test.get_user_credentials(
32+
["scopes"],
33+
"some-client-id",
34+
"shh-secret",
35+
)
36+
37+
mock_flow.from_client_config.assert_called_once_with(
38+
mock.ANY,
39+
scopes=["scopes"],
40+
)
41+
actual_client_config = mock_flow.from_client_config.call_args[0][0]
42+
assert actual_client_config["installed"]["client_id"] == "some-client-id"
43+
assert actual_client_config["installed"]["client_secret"] == "shh-secret"
44+
mock_flow_instance.run_console.assert_called_once()

0 commit comments

Comments
 (0)