Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8bb93f8

Browse filesBrowse files
author
Jon Wayne Parrott
authored
Add samples for auth (GoogleCloudPlatform#969)
1 parent 1c89ff4 commit 8bb93f8
Copy full SHA for 8bb93f8

File tree

Expand file treeCollapse file tree

9 files changed

+312
-0
lines changed
Filter options
Expand file treeCollapse file tree

9 files changed

+312
-0
lines changed

‎auth/api-client/requirements.txt

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-api-python-client==1.6.2
2+
google-auth-httplib2==0.0.2
3+
google-auth==1.0.1

‎auth/api-client/snippets.py

Copy file name to clipboard
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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+
"""Demonstrates how to authenticate to Google Cloud Platform APIs using
16+
the Google API Client."""
17+
18+
import argparse
19+
20+
21+
def implicit(project):
22+
import googleapiclient.discovery
23+
24+
# If you don't specify credentials when constructing the client, the
25+
# client library will look for credentials in the environment.
26+
storage_client = googleapiclient.discovery.build('storage', 'v1')
27+
28+
# Make an authenticated API request
29+
buckets = storage_client.buckets().list(project=project).execute()
30+
print(buckets)
31+
32+
33+
def explicit(project):
34+
from google.oauth2 import service_account
35+
import googleapiclient.discovery
36+
37+
# Construct service account credentials using the service account key
38+
# file.
39+
credentials = service_account.Credentials.from_service_account_file(
40+
'service_account.json')
41+
42+
# Explicitly pass the credentials to the client library.
43+
storage_client = googleapiclient.discovery.build(
44+
'storage', 'v1', credentials=credentials)
45+
46+
# Make an authenticated API request
47+
buckets = storage_client.buckets().list(project=project).execute()
48+
print(buckets)
49+
50+
51+
if __name__ == '__main__':
52+
parser = argparse.ArgumentParser(
53+
description=__doc__,
54+
formatter_class=argparse.RawDescriptionHelpFormatter)
55+
parser.add_argument('project')
56+
57+
subparsers = parser.add_subparsers(dest='command')
58+
subparsers.add_parser('implicit', help=implicit.__doc__)
59+
subparsers.add_parser('explicit', help=explicit.__doc__)
60+
61+
args = parser.parse_args()
62+
63+
if args.command == 'implicit':
64+
implicit(args.project)
65+
elif args.command == 'explicit':
66+
explicit(args.project)

‎auth/api-client/snippets_test.py

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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 os
16+
17+
import mock
18+
19+
import snippets
20+
21+
22+
def test_implicit():
23+
snippets.implicit(os.environ['GCLOUD_PROJECT'])
24+
25+
26+
def test_explicit():
27+
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as creds_file:
28+
creds_file_data = creds_file.read()
29+
30+
open_mock = mock.mock_open(read_data=creds_file_data)
31+
32+
with mock.patch('io.open', open_mock):
33+
snippets.explicit(os.environ['GCLOUD_PROJECT'])

‎auth/cloud-client/requirements.txt

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-storage==1.1.1

‎auth/cloud-client/snippets.py

Copy file name to clipboard
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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+
"""Demonstrates how to authenticate to Google Cloud Platform APIs using
16+
the Google Cloud Client Libraries."""
17+
18+
import argparse
19+
20+
21+
def implicit():
22+
from google.cloud import storage
23+
24+
# If you don't specify credentials when constructing the client, the
25+
# client library will look for credentials in the environment.
26+
storage_client = storage.Client()
27+
28+
# Make an authenticated API request
29+
buckets = list(storage_client.list_buckets())
30+
print(buckets)
31+
32+
33+
def explicit():
34+
from google.cloud import storage
35+
36+
# Explicitly use service account credentials by specifying the private key
37+
# file. All clients in google-cloud-python have this helper, see
38+
# https://google-cloud-python.readthedocs.io/en/latest/core/modules.html
39+
# #google.cloud.client.Client.from_service_account_json
40+
storage_client = storage.Client.from_service_account_json(
41+
'service_account.json')
42+
43+
# Make an authenticated API request
44+
buckets = list(storage_client.list_buckets())
45+
print(buckets)
46+
47+
48+
if __name__ == '__main__':
49+
parser = argparse.ArgumentParser(
50+
description=__doc__,
51+
formatter_class=argparse.RawDescriptionHelpFormatter)
52+
53+
subparsers = parser.add_subparsers(dest='command')
54+
subparsers.add_parser('implicit', help=implicit.__doc__)
55+
subparsers.add_parser('explicit', help=explicit.__doc__)
56+
57+
args = parser.parse_args()
58+
59+
if args.command == 'implicit':
60+
implicit()
61+
elif args.command == 'explicit':
62+
explicit()

‎auth/cloud-client/snippets_test.py

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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 os
16+
17+
import mock
18+
19+
import snippets
20+
21+
22+
def test_implicit():
23+
snippets.implicit()
24+
25+
26+
def test_explicit():
27+
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as creds_file:
28+
creds_file_data = creds_file.read()
29+
30+
open_mock = mock.mock_open(read_data=creds_file_data)
31+
32+
with mock.patch('io.open', open_mock):
33+
snippets.explicit()

‎auth/http-client/requirements.txt

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.17.3
2+
google-auth==1.0.1

‎auth/http-client/snippets.py

Copy file name to clipboard
+79Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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+
"""Demonstrates how to authenticate to Google Cloud Platform APIs using the
16+
Requests HTTP library."""
17+
18+
import argparse
19+
20+
21+
def implicit():
22+
import google.auth
23+
from google.auth.transport import requests
24+
25+
# Get the credentials and project ID from the environment.
26+
credentials, project = google.auth.default(
27+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
28+
29+
# Create a requests Session object with the credentials.
30+
session = requests.AuthorizedSession(credentials)
31+
32+
# Make an authenticated API request
33+
response = session.get(
34+
'https://www.googleapis.com/storage/v1/b'.format(project),
35+
params={'project': project})
36+
response.raise_for_status()
37+
buckets = response.json()
38+
print(buckets)
39+
40+
41+
def explicit(project):
42+
from google.auth.transport import requests
43+
from google.oauth2 import service_account
44+
45+
# Construct service account credentials using the service account key
46+
# file.
47+
credentials = service_account.Credentials.from_service_account_file(
48+
'service_account.json')
49+
credentials = credentials.with_scopes(
50+
['https://www.googleapis.com/auth/cloud-platform'])
51+
52+
# Create a requests Session object with the credentials.
53+
session = requests.AuthorizedSession(credentials)
54+
55+
# Make an authenticated API request
56+
response = session.get(
57+
'https://www.googleapis.com/storage/v1/b'.format(project),
58+
params={'project': project})
59+
response.raise_for_status()
60+
buckets = response.json()
61+
print(buckets)
62+
63+
64+
if __name__ == '__main__':
65+
parser = argparse.ArgumentParser(
66+
description=__doc__,
67+
formatter_class=argparse.RawDescriptionHelpFormatter)
68+
69+
subparsers = parser.add_subparsers(dest='command')
70+
subparsers.add_parser('implicit', help=implicit.__doc__)
71+
explicit_parser = subparsers.add_parser('explicit', help=explicit.__doc__)
72+
explicit_parser.add_argument('project')
73+
74+
args = parser.parse_args()
75+
76+
if args.command == 'implicit':
77+
implicit()
78+
elif args.command == 'explicit':
79+
explicit(args.project)

‎auth/http-client/snippets_test.py

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
# http://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 os
16+
17+
import mock
18+
19+
import snippets
20+
21+
22+
def test_implicit():
23+
snippets.implicit()
24+
25+
26+
def test_explicit():
27+
with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as creds_file:
28+
creds_file_data = creds_file.read()
29+
30+
open_mock = mock.mock_open(read_data=creds_file_data)
31+
32+
with mock.patch('io.open', open_mock):
33+
snippets.explicit(os.environ['GCLOUD_PROJECT'])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.