def _maybe_call_get_oauth_user(_scope=None): """Makes an GetOAuthUser RPC and stores the results in os.environ. This method will only make the RPC if 'OAUTH_ERROR_CODE' has not already been set or 'OAUTH_LAST_SCOPE' is different to _scope. """ if ('OAUTH_ERROR_CODE' not in os.environ or os.environ.get('OAUTH_LAST_SCOPE', None) != _scope): req = user_service_pb.GetOAuthUserRequest() if _scope: req.set_scope(_scope) resp = user_service_pb.GetOAuthUserResponse() try: apiproxy_stub_map.MakeSyncCall('user', 'GetOAuthUser', req, resp) os.environ['OAUTH_EMAIL'] = resp.email() os.environ['OAUTH_AUTH_DOMAIN'] = resp.auth_domain() os.environ['OAUTH_USER_ID'] = resp.user_id() if resp.is_admin(): os.environ['OAUTH_IS_ADMIN'] = '1' else: os.environ['OAUTH_IS_ADMIN'] = '0' os.environ['OAUTH_ERROR_CODE'] = '' except apiproxy_errors.ApplicationError, e: os.environ['OAUTH_ERROR_CODE'] = str(e.application_error) if _scope: os.environ['OAUTH_LAST_SCOPE'] = _scope else: os.environ.pop('OAUTH_LAST_SCOPE', None)
def _maybe_call_get_oauth_user(scope): """Makes an GetOAuthUser RPC and stores the results in os.environ. This method will only make the RPC if 'OAUTH_ERROR_CODE' has not already been set or 'OAUTH_LAST_SCOPE' is different to str(_scopes). Args: scope: The custom OAuth scope or an iterable of scopes at least one of which is accepted. """ if not scope: scope_str = '' elif isinstance(scope, basestring): scope_str = scope else: scope_str = str(sorted(scope)) if ('OAUTH_ERROR_CODE' not in os.environ or os.environ.get('OAUTH_LAST_SCOPE', None) != scope_str or os.environ.get('TESTONLY_OAUTH_SKIP_CACHE')): req = user_service_pb.GetOAuthUserRequest() if scope: if isinstance(scope, basestring): req.add_scopes(scope) else: req.scopes_list().extend(scope) resp = user_service_pb.GetOAuthUserResponse() try: apiproxy_stub_map.MakeSyncCall('user', 'GetOAuthUser', req, resp) os.environ['OAUTH_EMAIL'] = resp.email() os.environ['OAUTH_AUTH_DOMAIN'] = resp.auth_domain() os.environ['OAUTH_USER_ID'] = resp.user_id() os.environ['OAUTH_CLIENT_ID'] = resp.client_id() os.environ['OAUTH_AUTHORIZED_SCOPES'] = cPickle.dumps( list(resp.scopes_list()), cPickle.HIGHEST_PROTOCOL) if resp.is_admin(): os.environ['OAUTH_IS_ADMIN'] = '1' else: os.environ['OAUTH_IS_ADMIN'] = '0' os.environ['OAUTH_ERROR_CODE'] = '' except apiproxy_errors.ApplicationError as e: os.environ['OAUTH_ERROR_CODE'] = str(e.application_error) os.environ['OAUTH_ERROR_DETAIL'] = e.error_detail os.environ['OAUTH_LAST_SCOPE'] = scope_str _maybe_raise_exception()