1

I am trying to automate a scheduled report using python from DV360.

As I understand the documentation you need to use the DBM api to do this.

To start it says:

  1. enable the DBM api in the google console - done.
  2. Once you have enabled your DBMI api, it will automatically redirect you to the DBM credentials section and tell you what existing credentials in your project are compatible with the api.

• I have an exiting service account credential that is compatible with the api. • I have copied the service account email and have set up a user profile for the email in DV360 with admin access

My code below is as follows:

#read in python cleansing modules

import pandas as pd
from pandas.io import gbq
import numpy as np
import datetime 


#read in service account oauth
from google.oauth2 import service_account


#read in other google modules needed 
from google.cloud import bigquery
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials



SCOPES = ["https://www.googleapis.com/auth/spreadsheets", 
          "https://www.googleapis.com/auth/bigquery",
          "https://www.googleapis.com/doubleclickbidmanager"]
SERVICE_ACCOUNT_FILE = 'pathtoserviceaccountcreds.json'

creds = None
creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)


dbm = build('doubleclickbidmanager', 'v1.1', credentials = creds)

body={
    'params': {
        'type': 'TYPE_GENERAL',
        'metrics': [
            'METRIC_IMPRESSIONS', 'METRIC_CLICKS', 
            'METRIC_REVENUE_ADVERTISER'
        ],
        'groupBys': [
            'ADVERTISER', 'ADVERTISER_ID', 'INSERTION_ORDER','INSERTION_ORDER_ID', 
            'ADVERTISER_CURRENCY'
        ],
        'filters': [{
            'type': 'FILTER_ADVERTISER',
            'value': 'XXXXXXXXX'
        }],
    },
    'metadata': {
        'title': 'DV360 Automation API-generated report',
        'dataRange': 'LAST_90_DAYS',
        'format': 'csv'
    },
    'schedule': {
        'frequency': 'ONE_TIME'
    }
}

When I run the code up to dbm which uses the build method to configure the google apis client, it appears it is correctly configured the

out put it returns is googleapiclient.discovery.Resource at 0x1200e8d90

However when I try to run the below code to try and create a query I get the below error

dbm.queries().createquery(body=report_definition).execute()

RefreshError: ('No access token in response.', {'id_token': 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjI3YzcyNjE5ZDA5MzVhMjkwYzQxYzNmMDEwMTY3MTM4Njg1ZjdlNTMiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9hdXRoL3NwcmVhZHNoZWV0cyxodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9hdXRoL2JpZ3F1ZXJ5LGh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvZGlzcGxheS12aWRlbyxodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9kb3VibGVjbGlja2JpZG1hbmFnZXIiLCJhenAiOiJrYWxpYmVyZ29vZ2xlc2hlZXRzQG5vbWFkaWMtYXNzZXQtMzIwNjEzLmlhbS5nc2VydmljZWFjY291bnQuY29tIiwiZW1haWwiOiJrYWxpYmVyZ29vZ2xlc2hlZXRzQG5vbWFkaWMtYXNzZXQtMzIwNjEzLmlhbS5nc2VydmljZWFjY291bnQuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImV4cCI6MTYzNjM1MzQ2NCwiaWF0IjoxNjM2MzQ5ODY0LCJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJzdWIiOiIxMTAxMjA3MjMwODQ4MTI1MjE1ODUifQ.fBoy73zflwNMpgfzNErJZz7vhb4xmUJwJp9teHdemgkSDVPb5IwXiZA2JL9MSyL7glB6eaWHQRni8uJTWp_nxQlUzfH6G7uBf49F9eMxCMTgu_VbvHh0RENlxmbW9w2jJ-ezSNHHK4qlgr0Rb6dqNEKJV8GiIasxO5jhb3CM5-nwDN055dSKyWEyk98u9_DarnE1suv-e_RwcqhpsVCR0sl45Z-dp0k52uSSbF14iv0VbVyQfSfN7II2b0miNiCbDlmN4Ak-h3pP3w5b0LrUYhX0Jvq_83gz35BV2LkwQ9fDx6r8_tymrGjmrhC-QzHD8a4a1whUcpW4Ygd5sXkoFA'})

There are 2 threads from all the DBM/dv360 threads existing on stack that most closely fit my question, but none of them are conclusive and they are all over a year old

you can find the cases:

  1. Doubleclickbid manager api using service account

  2. How to use double click bid manager(DBM) API in python

For reference I have also used the super admin in the google console to give domain wide access to the service account I am using, with the scopes above, as per the documentation link below: https://developers.google.com/identity/protocols/oauth2/service-account

I have also tried to run a version where i configure delegated credentials in which the example email i used has both access admin user access to the DV360 and my service account

delegated_creds = creds.with_subject('example@.email.com')

again when i run build using delegated_creds in the place of the credentials it looks like it correctly configures

dbm = build('doubleclickbidmanager', 'v1.1', credentials = delegated_creds)
 

it returns googleapiclient.discovery.Resource at 0x11a89f5b0

•but again the code breaks when u run the .execute() method.

dbm.queries().listqueries().execute()

returning the error: HttpAccessTokenRefreshError: invalid_scope: https://www.googleapis.com/auth/spreadsheets is not a valid audience string.

•it repeats the error even if i delete the other scopes and just leave

RefreshError: ('invalid_scope: https://www.googleapis.com/doubleclickbidmanager is not a valid audience string.', {'error': 'invalid_scope', 'error_description': 'https://www.googleapis.com/doubleclickbidmanager is not a valid audience string.'})

for the record it will correctly configure the dbm client if i run

dbm.queries().listqueries().

it returns googleapiclient.http.HttpRequest at 0x11aab6170

Can someone please help?

also the path to my credentials is correct bc i can use the google sheets api fine with this.

bosh
  • 11
  • 2

0 Answers0