From cb6d8918813cb09eed6834b59cafd2999bedbda9 Mon Sep 17 00:00:00 2001 From: JacobMoshenko Date: Fri, 8 Jul 2011 13:35:15 -0400 Subject: [PATCH] Remove user_agent from app engine helpers. --- oauth2client/appengine.py | 15 ++++++--------- oauth2client/client.py | 20 ++++++++++++++------ samples/appengine_with_robots/main.py | 3 +-- tests/test_oauth2client_appengine.py | 7 ++----- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/oauth2client/appengine.py b/oauth2client/appengine.py index a9766c7..439a579 100644 --- a/oauth2client/appengine.py +++ b/oauth2client/appengine.py @@ -71,7 +71,7 @@ class AppAssertionCredentials(AssertionCredentials): AssertionFlowCredentials objects may be safely pickled and unpickled. """ - def __init__(self, scope, user_agent, + def __init__(self, scope, audience='https://accounts.google.com/o/oauth2/token', assertion_type='http://oauth.net/grant_type/jwt/1.0/bearer', token_uri='https://accounts.google.com/o/oauth2/token', **kwargs): @@ -79,7 +79,6 @@ class AppAssertionCredentials(AssertionCredentials): Args: scope: string, scope of the credentials being requested. - user_agent: string, The HTTP User-Agent to provide for this application. audience: string, The audience, or verifier of the assertion. For convenience defaults to Google's audience. assertion_type: string, Type name that will identify the format of the @@ -94,7 +93,7 @@ class AppAssertionCredentials(AssertionCredentials): super(AppAssertionCredentials, self).__init__( assertion_type, - user_agent, + None, token_uri) def _generate_assertion(self): @@ -261,8 +260,7 @@ class OAuth2Decorator(object): decorator = OAuth2Decorator( client_id='837...ent.com', client_secret='Qh...wwI', - scope='https://www.googleapis.com/auth/buzz', - user_agent='my-sample-app/1.0') + scope='https://www.googleapis.com/auth/buzz') class MainHandler(webapp.RequestHandler): @@ -275,7 +273,7 @@ class OAuth2Decorator(object): """ - def __init__(self, client_id, client_secret, scope, user_agent, + def __init__(self, client_id, client_secret, scope, auth_uri='https://accounts.google.com/o/oauth2/auth', token_uri='https://accounts.google.com/o/oauth2/token'): @@ -285,14 +283,13 @@ class OAuth2Decorator(object): client_id: string, client identifier. client_secret: string client secret. scope: string, scope of the credentials being requested. - user_agent: string, HTTP User-Agent to provide for this application. auth_uri: string, URI for authorization endpoint. For convenience defaults to Google's endpoints but any OAuth 2.0 provider can be used. token_uri: string, URI for token endpoint. For convenience defaults to Google's endpoints but any OAuth 2.0 provider can be used. """ - self.flow = OAuth2WebServerFlow(client_id, client_secret, scope, - user_agent, auth_uri, token_uri) + self.flow = OAuth2WebServerFlow(client_id, client_secret, scope, None, + auth_uri, token_uri) self.credentials = None self._request_handler = None diff --git a/oauth2client/client.py b/oauth2client/client.py index 523a185..f547428 100644 --- a/oauth2client/client.py +++ b/oauth2client/client.py @@ -202,9 +202,12 @@ class OAuth2Credentials(Credentials): """Generate the headers that will be used in the refresh request """ headers = { - 'user-agent': self.user_agent, 'content-type': 'application/x-www-form-urlencoded', } + + if self.user_agent is not None: + headers['user-agent'] = self.user_agent + return headers def _refresh(self, http_request): @@ -289,10 +292,12 @@ class OAuth2Credentials(Credentials): if headers == None: headers = {} headers['authorization'] = 'OAuth ' + self.access_token - if 'user-agent' in headers: - headers['user-agent'] = self.user_agent + ' ' + headers['user-agent'] - else: - headers['user-agent'] = self.user_agent + + if self.user_agent is not None: + if 'user-agent' in headers: + headers['user-agent'] = self.user_agent + ' ' + headers['user-agent'] + else: + headers['user-agent'] = self.user_agent resp, content = request_orig(uri, method, body, headers, redirections, connection_type) @@ -494,9 +499,12 @@ class OAuth2WebServerFlow(Flow): 'scope': self.scope, }) headers = { - 'user-agent': self.user_agent, 'content-type': 'application/x-www-form-urlencoded', } + + if self.user_agent is not None: + headers['user-agent'] = self.user_agent + if http is None: http = httplib2.Http() resp, content = http.request(self.token_uri, method='POST', body=body, diff --git a/samples/appengine_with_robots/main.py b/samples/appengine_with_robots/main.py index 01ee3a8..5bfe13d 100644 --- a/samples/appengine_with_robots/main.py +++ b/samples/appengine_with_robots/main.py @@ -40,8 +40,7 @@ from google.appengine.ext.webapp.util import run_wsgi_app from oauth2client.appengine import AppAssertionCredentials credentials = AppAssertionCredentials( - scope='https://www.googleapis.com/auth/urlshortener', - user_agent='my-sample-app/1.0') + scope='https://www.googleapis.com/auth/urlshortener') http = credentials.authorize(httplib2.Http(memcache)) service = build("urlshortener", "v1", http=http) diff --git a/tests/test_oauth2client_appengine.py b/tests/test_oauth2client_appengine.py index 7f370b3..fd06668 100644 --- a/tests/test_oauth2client_appengine.py +++ b/tests/test_oauth2client_appengine.py @@ -93,9 +93,7 @@ class TestAppAssertionCredentials(unittest.TestCase): app_identity_stub) self.scope = "http://www.googleapis.com/scope" - user_agent = "hal/3.0" - - self.credentials = AppAssertionCredentials(self.scope, user_agent) + self.credentials = AppAssertionCredentials(self.scope) def test_assertion(self): assertion = self.credentials._generate_assertion() @@ -133,8 +131,7 @@ class DecoratorTests(unittest.TestCase): decorator = OAuth2Decorator(client_id='foo_client_id', client_secret='foo_client_secret', - scope='foo_scope', - user_agent='foo_user_agent') + scope='foo_scope') self.decorator = decorator class TestRequiredHandler(webapp.RequestHandler):