I have a Cloud Function (CF) which is getting TRIGGERED after a file is copied into GCS. The aim is copy this media file in Campaign Manager 360.
I have my oAuth 2.0 Credential created which i want to use ( after the ‘flow’ statement. ) But my CF fails saying “Name: client is not defined”
And although the last part of the 5-6 lines of code is me just trying to list something, the actual aim in THAT PART is to upload a media file, which is currently in GCS, into Campaign Manager 360. How do i POST it to CM360, from within the CF ?
https://developers.google.com/doubleclick-advertisers/upload?hl=en
<code>import functions_framework
import json
import hashlib
from flask import abort
# Triggered by a change in a storage bucket
@functions_framework.cloud_event
def creative_to_gcs(cloud_event):
data = cloud_event.data
event_id = cloud_event["id"]
event_type = cloud_event["type"]
bucket = data["bucket"]
name = data["name"]
metageneration = data["metageneration"]
timeCreated = data["timeCreated"]
updated = data["updated"]
print(f"Event ID: {event_id}")
print(f"Event type: {event_type}")
print(f"Bucket: {bucket}")
print(f"File: {name}")
print(f"Metageneration: {metageneration}")
print(f"Created: {timeCreated}")
print(f"Updated: {updated}")
flow = client.flow_from_clientsecrets(path_to_client_secrets_file, scope=OAUTH_SCOPES)
storage = Storage(CREDENTIAL_STORE_FILE)
credentials = storage.get()
# If no credentials were found, go through the authorization process and
# persist credentials to the credential store.
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, tools.argparser.parse_known_args()[0])
# Use the credentials to authorize an httplib2.Http instance.
http = credentials.authorize(httplib2.Http())
# Construct a service object via the discovery service.
service = discovery.build('dfareporting', 'v4', http=http)
# 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'])) ```
</code>
<code>import functions_framework
import json
import hashlib
from flask import abort
# Triggered by a change in a storage bucket
@functions_framework.cloud_event
def creative_to_gcs(cloud_event):
data = cloud_event.data
event_id = cloud_event["id"]
event_type = cloud_event["type"]
bucket = data["bucket"]
name = data["name"]
metageneration = data["metageneration"]
timeCreated = data["timeCreated"]
updated = data["updated"]
print(f"Event ID: {event_id}")
print(f"Event type: {event_type}")
print(f"Bucket: {bucket}")
print(f"File: {name}")
print(f"Metageneration: {metageneration}")
print(f"Created: {timeCreated}")
print(f"Updated: {updated}")
flow = client.flow_from_clientsecrets(path_to_client_secrets_file, scope=OAUTH_SCOPES)
storage = Storage(CREDENTIAL_STORE_FILE)
credentials = storage.get()
# If no credentials were found, go through the authorization process and
# persist credentials to the credential store.
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, tools.argparser.parse_known_args()[0])
# Use the credentials to authorize an httplib2.Http instance.
http = credentials.authorize(httplib2.Http())
# Construct a service object via the discovery service.
service = discovery.build('dfareporting', 'v4', http=http)
# 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'])) ```
</code>
import functions_framework
import json
import hashlib
from flask import abort
# Triggered by a change in a storage bucket
@functions_framework.cloud_event
def creative_to_gcs(cloud_event):
data = cloud_event.data
event_id = cloud_event["id"]
event_type = cloud_event["type"]
bucket = data["bucket"]
name = data["name"]
metageneration = data["metageneration"]
timeCreated = data["timeCreated"]
updated = data["updated"]
print(f"Event ID: {event_id}")
print(f"Event type: {event_type}")
print(f"Bucket: {bucket}")
print(f"File: {name}")
print(f"Metageneration: {metageneration}")
print(f"Created: {timeCreated}")
print(f"Updated: {updated}")
flow = client.flow_from_clientsecrets(path_to_client_secrets_file, scope=OAUTH_SCOPES)
storage = Storage(CREDENTIAL_STORE_FILE)
credentials = storage.get()
# If no credentials were found, go through the authorization process and
# persist credentials to the credential store.
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, tools.argparser.parse_known_args()[0])
# Use the credentials to authorize an httplib2.Http instance.
http = credentials.authorize(httplib2.Http())
# Construct a service object via the discovery service.
service = discovery.build('dfareporting', 'v4', http=http)
# 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'])) ```