Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 647ab8a

Browse files
committed
Handle two versions of oauth2client (>=2.0.0 and 1.5.2)
----Release Notes---- [] ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=117493674
1 parent dfcfded commit 647ab8a

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

  • google/cloud/dataflow/internal

google/cloud/dataflow/internal/auth.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424

2525
from oauth2client.client import OAuth2Credentials
26-
from oauth2client.client import SignedJwtAssertionCredentials
2726

2827
from google.cloud.dataflow.utils import processes
2928
from google.cloud.dataflow.utils.options import GoogleCloudOptions
@@ -124,19 +123,33 @@ def get_service_credentials():
124123
raise Exception('key file not provided for service account.')
125124
if not os.path.exists(google_cloud_options.service_account_key_file):
126125
raise Exception('Specified service account key file does not exist.')
127-
with file(google_cloud_options.service_account_key_file) as f:
128-
service_account_key = f.read()
129126
client_scopes = [
130127
'https://www.googleapis.com/auth/bigquery',
131128
'https://www.googleapis.com/auth/cloud-platform',
132129
'https://www.googleapis.com/auth/devstorage.full_control',
133130
'https://www.googleapis.com/auth/userinfo.email',
134131
'https://www.googleapis.com/auth/datastore'
135132
]
136-
return SignedJwtAssertionCredentials(
137-
google_cloud_options.service_account_name,
138-
service_account_key,
139-
client_scopes,
140-
user_agent=user_agent)
133+
134+
# The following code uses oauth2client >=2.0.0 functionality and if this
135+
# is not available due to import errors will use 1.5.2 functionality.
136+
# TODO(silviuc): Remove the 1.5.2 when dependencies have been updated.
137+
try:
138+
from oauth2client.service_account import ServiceAccountCredentials
139+
return ServiceAccountCredentials.from_p12_keyfile(
140+
google_cloud_options.service_account_name,
141+
google_cloud_options.service_account_key_file,
142+
client_scopes,
143+
user_agent=user_agent)
144+
except ImportError:
145+
with file(google_cloud_options.service_account_key_file) as f:
146+
service_account_key = f.read()
147+
from oauth2client.client import SignedJwtAssertionCredentials
148+
return SignedJwtAssertionCredentials(
149+
google_cloud_options.service_account_name,
150+
service_account_key,
151+
client_scopes,
152+
user_agent=user_agent)
153+
141154
else:
142155
return _GCloudWrapperCredentials(user_agent)

0 commit comments

Comments
 (0)