Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
profileId=profile_id, creativeFieldId=field_id)
while True:
# Execute request and print response.
response = request.execute()
for value in response['creativeFieldValues']:
print ('Found creative field value with ID %s and value "%s".'
% (value['id'], value['value']))
if response['creativeFieldValues'] and response['nextPageToken']:
request = service.creativeFieldValues().list_next(request, response)
else:
break
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
# Construct and save advertiser.
advertiser = {
'name': 'Test Advertiser',
'status': 'APPROVED'
}
request = service.advertisers().insert(
profileId=profile_id, body=advertiser)
# Execute request and print response.
response = request.execute()
print ('Created advertiser with ID %s and name "%s".'
% (response['id'], response['name']))
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
request = service.conversions().batchinsert(profileId=profile_id,
body=request_body)
response = request.execute()
if not response['hasFailures']:
print ('Successfully inserted conversion for mobile device ID %s.'
% mobile_device_id)
else:
print ('Error(s) inserting conversion for mobile device ID %s.'
% mobile_device_id)
status = response['status'][0]
for error in status['errors']:
print '\t[%s]: %s' % (error['code'], error['message'])
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
def get_data(self, **kwargs):
# Documentation:
# https://google-api-client-libraries.appspot.com/documentation/analytics/v3/python/latest/analytics_v3.data.ga.html
ga = self.service.data().ga()
try:
response = ga.get(ids='ga:%s' % self.profile_id,
**kwargs).execute()
except TypeError as e:
raise ArgumentError("Google Analytics Error: %s"
% str(e))
except HttpError as e:
raise BrowserError("Google Analytics HTTP Error: %s"
% str(e))
except AccessTokenRefreshError as e:
raise NotImplementedError("Re-authorization not implemented yet")
return response
request = service.contentCategories().list(profileId=profile_id)
while True:
# Execute request and print response.
response = request.execute()
for category in response['contentCategories']:
print ('Found content category with ID %s and name "%s".'
% (category['id'], category['name']))
if response['contentCategories'] and response['nextPageToken']:
request = service.contentCategories().list_next(request, response)
else:
break
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
def retrieve_single_document_meta(self, doc_id, is_folder=False):
try:
meta = self.drive_service.files().get(fileId=doc_id).execute()
klass = Folder if is_folder else Document
return klass(meta, self.folders)
except AccessTokenRefreshError:
# most likely 403
raise client_module.ExpiredTokenException
request = service.campaigns().list(profileId=profile_id)
while True:
# Execute request and print response.
response = request.execute()
for campaign in response['campaigns']:
print ('Found campaign with ID %s and name "%s".'
% (campaign['id'], campaign['name']))
if response['campaigns'] and response['nextPageToken']:
request = service.campaigns().list_next(request, response)
else:
break
except client.AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
# Construct a service object via the discovery service.
service = discovery.build('dfareporting', 'v3.2', http=http)
try:
# Construct the request.
request = service.userProfiles().list()
# Execute request and print response.
response = request.execute()
for profile in response['items']:
print('Found user profile with ID %s and user name "%s".' %
(profile['profileId'], profile['userName']))
except client.AccessTokenRefreshError:
print('The credentials have been revoked or expired, please re-run the '
'application to re-authorize')
def SendMessages(post_data):
storage = StorageByKeyName(AdminCredentialsModel,
'theadminaccount',
'credentials')
credentials = storage.get()
if credentials:
try:
api_http = credentials.authorize(httplib2.Http())
for data in post_data:
api_http.request(
'https://www.googleapis.com/gcm_for_chrome/v1/messages',
'POST',
body=simplejson.dumps(data),
headers={'Content-Type': 'application/json'})
except AccessTokenRefreshError:
logger.warning('Unable to refresh the Push Messaging access token!')