#NEWLINE# def _Dynamic_GetAccessToken( self , request , response ) : #NEWLINE# #INDENT# 'Implementation of AppIdentityService::GetAccessToken.\n\n    This API requires internet access.\n\n    Raises:\n      apiproxy_errors.ApplicationError: If unexpected response from\n                                        Google server.\n    ' #NEWLINE# scope = ' ' . join ( request . scope_list ( ) ) #NEWLINE# with self . __access_token_cache_lock : #NEWLINE# #INDENT# rv = self . __access_token_cache . get ( scope , None ) #NEWLINE# #UNINDENT# now = int ( time . time ( ) ) #NEWLINE# if ( not ( rv and ( rv [ 'expires' ] > ( now + 60 ) ) ) ) : #NEWLINE# #INDENT# assertion_input = ( '%s.%s' % ( base64 . urlsafe_b64encode ( json . dumps ( { #NEWLINE# #INDENT# 'alg' : 'RS256' , #NEWLINE# 'typ' : 'JWT' , #NEWLINE# #UNINDENT# } ) . encode ( 'UTF-8' ) ) . rstrip ( '=' ) , base64 . urlsafe_b64encode ( json . dumps ( { #NEWLINE# #INDENT# 'iss' : self . __email_address , #NEWLINE# 'scope' : scope , #NEWLINE# 'aud' : self . __oauth_url , #NEWLINE# 'exp' : ( now + ( 60 * 60 ) ) , #NEWLINE# 'iat' : now , #NEWLINE# #UNINDENT# } ) . encode ( 'UTF-8' ) ) . rstrip ( '=' ) ) ) #NEWLINE# signature = base64 . urlsafe_b64encode ( rsa . pkcs1 . sign ( assertion_input , self . __private_key , 'SHA-256' ) ) . rstrip ( '=' ) #NEWLINE# message = urllib . urlencode ( { #NEWLINE# #INDENT# 'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer' , #NEWLINE# 'assertion' : ( '%s.%s' % ( assertion_input , signature ) ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# resp = urlfetch . fetch ( url = self . __oauth_url , validate_certificate = True , payload = message , method = urlfetch . POST , headers = { #NEWLINE# #INDENT# 'Content-Type' : 'application/x-www-form-urlencoded' , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( response . status_code != 200 ) : #NEWLINE# #INDENT# raise apiproxy_errors . ApplicationError ( app_identity_service_pb . AppIdentityServiceError . UNKNOWN_ERROR , ( 'Error getting access token. Response code: %i, Content: %s' % ( resp . status_code , resp . content ) ) ) #NEWLINE# #UNINDENT# rv = json . loads ( resp . content ) #NEWLINE# rv [ 'expires' ] = ( now + int ( rv . get ( 'expires_in' , '0' ) ) ) #NEWLINE# with self . __access_token_cache_lock : #NEWLINE# #INDENT# self . __access_token_cache [ scope ] = rv #NEWLINE# #UNINDENT# #UNINDENT# response . set_access_token ( rv [ 'access_token' ] ) #NEWLINE# response . set_expiration_time ( rv [ 'expires' ] )