Skip to content

Commit f21866f

Browse files
author
Jon Wayne Parrott
authored
Remove oauth2client usage in endpoints samples (GoogleCloudPlatform#1002)
1 parent 10bf2af commit f21866f

File tree

7 files changed

+44
-36
lines changed

7 files changed

+44
-36
lines changed

endpoints/getting-started/clients/google-id-token-client.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@
1919

2020
import argparse
2121

22-
import oauth2client.client
23-
import oauth2client.file
24-
import oauth2client.tools
22+
import google_auth_oauthlib.flow
2523
import requests
2624
from six.moves import urllib
2725

2826

2927
def get_id_token(client_secrets_file, extra_args):
30-
storage = oauth2client.file.Storage('credentials.dat')
31-
credentials = storage.get()
28+
"""Obtains credentials from the user using OAuth 2.0 and then returns the
29+
ID token from those credentials."""
3230

33-
if not credentials or credentials.invalid:
34-
flow = oauth2client.client.flow_from_clientsecrets(
35-
client_secrets_file, scope='email')
36-
credentials = oauth2client.tools.run_flow(
37-
flow, storage, flags=extra_args)
31+
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
32+
client_secrets_file, scopes=['openid', 'email', 'profile'])
3833

39-
# The ID token is used by Cloud Endpoints, not the access token.
40-
id_token = credentials.token_response['id_token']
34+
# Run the OAuth 2.0 flow to obtain credentials from the user.
35+
flow.run_local_server()
36+
37+
# The credentials have both an access token and an ID token. Cloud
38+
# Endpoints uses the ID Token.
39+
id_token = flow.oauth2session.token['id_token']
4140

4241
return id_token
4342

@@ -67,8 +66,7 @@ def main(host, api_key, client_secrets_file, extra_args):
6766
if __name__ == '__main__':
6867
parser = argparse.ArgumentParser(
6968
description=__doc__,
70-
formatter_class=argparse.RawDescriptionHelpFormatter,
71-
parents=[oauth2client.tools.argparser])
69+
formatter_class=argparse.RawDescriptionHelpFormatter)
7270
parser.add_argument(
7371
'host', help='Your API host, e.g. https://your-project.appspot.com.')
7472
parser.add_argument(

endpoints/getting-started/clients/google-jwt-client.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,30 @@
2020
import argparse
2121
import time
2222

23-
import oauth2client.crypt
24-
from oauth2client.service_account import ServiceAccountCredentials
23+
import google.auth.crypt
24+
import google.auth.jwt
2525
import requests
2626
from six.moves import urllib
2727

2828

2929
def generate_jwt(service_account_file):
3030
"""Generates a signed JSON Web Token using a Google API Service Account."""
31-
credentials = ServiceAccountCredentials.from_json_keyfile_name(
31+
32+
# Note: this sample shows how to manually create the JWT for the purposes
33+
# of showing how the authentication works, but you can use
34+
# google.auth.jwt.Credentials to automatically create the JWT.
35+
# http://google-auth.readthedocs.io/en/latest/reference
36+
# /google.auth.jwt.html#google.auth.jwt.Credentials
37+
38+
signer = google.auth.crypt.RSASigner.from_service_account_file(
3239
service_account_file)
3340

3441
now = int(time.time())
42+
expires = now + 3600 # One hour in seconds
3543

3644
payload = {
3745
'iat': now,
38-
'exp': now + credentials.MAX_TOKEN_LIFETIME_SECS,
46+
'exp': expires,
3947
# aud must match 'audience' in the security configuration in your
4048
# swagger spec. It can be any string.
4149
'aud': 'echo.endpoints.sample.google.com',
@@ -47,10 +55,9 @@ def generate_jwt(service_account_file):
4755
'email': '[email protected]'
4856
}
4957

50-
signed_jwt = oauth2client.crypt.make_signed_jwt(
51-
credentials._signer, payload, key_id=credentials._private_key_id)
58+
jwt = google.auth.jwt.encode(signer, payload)
5259

53-
return signed_jwt
60+
return jwt
5461

5562

5663
def make_request(host, api_key, signed_jwt):

endpoints/getting-started/clients/service_to_service_gae_default/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def generate_jwt():
4949
"email": DEFAULT_SERVICE_ACCOUNT
5050
})
5151

52-
headerAndPayload = '{}.{}'.format(
52+
header_and_payload = '{}.{}'.format(
5353
base64.urlsafe_b64encode(header_json),
5454
base64.urlsafe_b64encode(payload_json))
55-
(key_name, signature) = app_identity.sign_blob(headerAndPayload)
55+
(key_name, signature) = app_identity.sign_blob(header_and_payload)
5656
signed_jwt = '{}.{}'.format(
57-
headerAndPayload,
57+
header_and_payload,
5858
base64.urlsafe_b64encode(signature))
5959

6060
return signed_jwt

endpoints/getting-started/clients/service_to_service_google_id_token/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def generate_jwt():
5050
"aud": "https://www.googleapis.com/oauth2/v4/token"
5151
})
5252

53-
headerAndPayload = '{}.{}'.format(
53+
header_and_payload = '{}.{}'.format(
5454
base64.urlsafe_b64encode(header_json),
5555
base64.urlsafe_b64encode(payload_json))
56-
(key_name, signature) = app_identity.sign_blob(headerAndPayload)
56+
(key_name, signature) = app_identity.sign_blob(header_and_payload)
5757
signed_jwt = '{}.{}'.format(
58-
headerAndPayload,
58+
header_and_payload,
5959
base64.urlsafe_b64encode(signature))
6060

6161
return signed_jwt

endpoints/getting-started/clients/service_to_service_non_default/main.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import json
2121
import time
2222

23+
import google.auth.app_engine
2324
import googleapiclient.discovery
24-
import httplib2
25-
from oauth2client.contrib.appengine import AppAssertionCredentials
2625
import webapp2
2726

2827
SERVICE_ACCOUNT_EMAIL = "YOUR-SERVICE-ACCOUNT-EMAIL"
@@ -33,11 +32,10 @@
3332

3433
def generate_jwt():
3534
"""Generates a signed JSON Web Token using a service account."""
36-
credentials = AppAssertionCredentials(
37-
'https://www.googleapis.com/auth/iam')
38-
http_auth = credentials.authorize(httplib2.Http())
35+
credentials = google.auth.app_engine.Credentials(
36+
scopes=['https://www.googleapis.com/auth/iam'])
3937
service = googleapiclient.discovery.build(
40-
serviceName='iam', version='v1', http=http_auth)
38+
serviceName='iam', version='v1', credentials=credentials)
4139

4240
now = int(time.time())
4341

@@ -58,16 +56,16 @@ def generate_jwt():
5856
"email": SERVICE_ACCOUNT_EMAIL
5957
})
6058

61-
headerAndPayload = '{}.{}'.format(
59+
header_and_payload = '{}.{}'.format(
6260
base64.urlsafe_b64encode(header_json),
6361
base64.urlsafe_b64encode(payload_json))
6462
slist = service.projects().serviceAccounts().signBlob(
6563
name=SERVICE_ACCOUNT,
66-
body={'bytesToSign': base64.b64encode(headerAndPayload)})
64+
body={'bytesToSign': base64.b64encode(header_and_payload)})
6765
res = slist.execute()
6866
signature = base64.urlsafe_b64encode(
6967
base64.decodestring(res['signature']))
70-
signed_jwt = '{}.{}'.format(headerAndPayload, signature)
68+
signed_jwt = '{}.{}'.format(header_and_payload, signature)
7169

7270
return signed_jwt
7371

Lines 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==1.0.1
3+
google-auth-httplib2==0.0.2

endpoints/getting-started/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ gunicorn==19.7.1
44
six==1.10.0
55
pyyaml==3.12
66
requests==2.18.1
7+
google-auth==1.0.0
8+
google-auth-oauthlib==0.1.0

0 commit comments

Comments
 (0)