Skip to content

Commit 956c120

Browse files
committed
Permit AppAssertionCredentials to specify the service account to use, Fixes issue googleapis#325.
1 parent 85db326 commit 956c120

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

oauth2client/appengine.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ def __init__(self, scope, **kwargs):
159159
Args:
160160
scope: string or iterable of strings, scope(s) of the credentials being
161161
requested.
162+
kwargs: optional keyword args, including:
163+
service_account_id: service account id of the application. If None or
164+
unspecified, the default service account for the app is used.
162165
"""
163166
self.scope = util.scopes_to_string(scope)
167+
self.service_account_id = kwargs.get('service_account_id', None)
164168

165169
# Assertion type is no longer used, but still in the parent class signature.
166170
super(AppAssertionCredentials, self).__init__(None)
@@ -186,7 +190,8 @@ def _refresh(self, http_request):
186190
"""
187191
try:
188192
scopes = self.scope.split()
189-
(token, _) = app_identity.get_access_token(scopes)
193+
(token, _) = app_identity.get_access_token(
194+
scopes, service_account_id=self.service_account_id)
190195
except app_identity.Error, e:
191196
raise AccessTokenRefreshError(str(e))
192197
self.access_token = token

tests/test_oauth2client_appengine.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ def test_get_access_token_on_refresh(self):
203203
'http://www.googleapis.com/scope http://www.googleapis.com/scope2',
204204
credentials.scope)
205205

206+
def test_custom_service_account(self):
207+
scope = "http://www.googleapis.com/scope"
208+
account_id = "service_account_name_2@appspot.com"
209+
m = mox.Mox()
210+
m.StubOutWithMock(app_identity, 'get_access_token')
211+
app_identity.get_access_token(
212+
[scope], service_account_id=account_id).AndReturn(('a_token_456', None))
213+
m.ReplayAll()
214+
215+
credentials = AppAssertionCredentials(scope, service_account_id=account_id)
216+
http = httplib2.Http()
217+
credentials.refresh(http)
218+
m.VerifyAll()
219+
m.UnsetStubs()
220+
self.assertEqual('a_token_456', credentials.access_token)
221+
self.assertEqual(scope, credentials.scope)
222+
206223

207224
class TestFlowModel(db.Model):
208225
flow = FlowProperty()

0 commit comments

Comments
 (0)