我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用google.appengine.api.app_identity.get_access_token()。
def _refresh(self, http_request): """Refreshes the access_token. Since the underlying App Engine app_identity implementation does its own caching we can skip all the storage hoops and just to a refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: AccessTokenRefreshError: When the refresh fails. """ try: scopes = self.scope.split() (token, _) = app_identity.get_access_token( scopes, service_account_id=self.service_account_id) except app_identity.Error as e: raise AccessTokenRefreshError(str(e)) self.access_token = token
def _refresh(self, http_request): """Refreshes the access_token. Since the underlying App Engine app_identity implementation does its own caching we can skip all the storage hoops and just to a refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: AccessTokenRefreshError: When the refresh fails. """ try: scopes = self.scope.split() (token, _) = app_identity.get_access_token(scopes) except app_identity.Error, e: raise AccessTokenRefreshError(str(e)) self.access_token = token
def _refresh(self, http): """Refreshes the access token. Since the underlying App Engine app_identity implementation does its own caching we can skip all the storage hoops and just to a refresh using the API. Args: http: unused HTTP object Raises: AccessTokenRefreshError: When the refresh fails. """ try: scopes = self.scope.split() (token, _) = app_identity.get_access_token( scopes, service_account_id=self.service_account_id) except app_identity.Error as e: raise client.AccessTokenRefreshError(str(e)) self.access_token = token
def _refresh(self, http_request): """Refreshes the access_token. Since the underlying App Engine app_identity implementation does its own caching we can skip all the storage hoops and just to a refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: AccessTokenRefreshError: When the refresh fails. """ try: (token, _) = app_identity.get_access_token(self.scope) except app_identity.Error, e: raise AccessTokenRefreshError(str(e)) self.access_token = token
def get(self): auth_token, _ = app_identity.get_access_token( 'https://www.googleapis.com/auth/cloud-platform') logging.info( 'Using token {} to represent identity {}'.format( auth_token, app_identity.get_service_account_name())) response = urlfetch.fetch( 'https://www.googleapis.com/storage/v1/b?project={}'.format( app_identity.get_application_id()), method=urlfetch.GET, headers={ 'Authorization': 'Bearer {}'.format(auth_token) } ) if response.status_code != 200: raise Exception( 'Call failed. Status code {}. Body {}'.format( response.status_code, response.content)) result = json.loads(response.content) self.response.headers['Content-Type'] = 'application/json' self.response.write(json.dumps(result, indent=2))
def list_bucket_files(bucket_name, prefix, max_keys=1000): """Returns a listing of of a bucket that matches the given prefix.""" scope = config.GoogleApiScope('devstorage.read_only') bucket_url = config.GsBucketURL(bucket_name) url = bucket_url + '?' query = [('max-keys', max_keys)] if prefix: query.append(('prefix', prefix)) url += urllib.urlencode(query) auth_token, _ = app_identity.get_access_token(scope) result = urlfetch.fetch(url, method=urlfetch.GET, headers={ 'Authorization': 'OAuth %s' % auth_token, 'x-goog-api-version': '2'}) if result and result.status_code == 200: doc = xml.dom.minidom.parseString(result.content) return [node.childNodes[0].data for node in doc.getElementsByTagName('Key')] raise BackupValidationError('Request to Google Cloud Storage failed')
def get_gs_object(bucket_name, path): """Returns a listing of of a bucket that matches the given prefix.""" scope = config.GoogleApiScope('devstorage.read_only') bucket_url = config.GsBucketURL(bucket_name) url = bucket_url + path auth_token, _ = app_identity.get_access_token(scope) result = urlfetch.fetch(url, method=urlfetch.GET, headers={ 'Authorization': 'OAuth %s' % auth_token, 'x-goog-api-version': '2'}) if result and result.status_code == 200: return result.content if result and result.status_code == 403: raise BackupValidationError( 'Requested path %s is not accessible/access denied' % url) if result and result.status_code == 404: raise BackupValidationError('Requested path %s was not found' % url) raise BackupValidationError('Error encountered accessing requested path %s' % url)
def __init__(self, scopes=None, service_account_id=None): """ Args: scopes (Sequence[str]): Scopes to request from the App Identity API. service_account_id (str): The service account ID passed into :func:`google.appengine.api.app_identity.get_access_token`. If not specified, the default application service account ID will be used. Raises: EnvironmentError: If the App Engine APIs are unavailable. """ # pylint: disable=missing-raises-doc # Pylint rightfully thinks EnvironmentError is OSError, but doesn't # realize it's a valid alias. if app_identity is None: raise EnvironmentError( 'The App Engine APIs are not available.') super(Credentials, self).__init__() self._scopes = scopes self._service_account_id = service_account_id self._signer = Signer()
def is_accessible_bucket_name(bucket_name): """Returns True if the application has access to the specified bucket.""" scope = config.GoogleApiScope('devstorage.read_write') bucket_url = config.GsBucketURL(bucket_name) auth_token, _ = app_identity.get_access_token(scope) result = urlfetch.fetch(bucket_url, method=urlfetch.HEAD, headers={ 'Authorization': 'OAuth %s' % auth_token, 'x-goog-api-version': '2'}) return result and result.status_code == 200
def refresh(self, request): # pylint: disable=unused-argument token, ttl = app_identity.get_access_token( self._scopes, self._service_account_id) expiry = datetime.datetime.utcfromtimestamp(ttl) self.token, self.expiry = token, expiry