Download report using python

968 views
Skip to first unread message

vaibhav

unread,
Oct 23, 2013, 3:22:09 AM10/23/13
to google-doubleclick-...@googlegroups.com
Hi, can someone please help me download a report using dfa api for python? I am very new to coding and I am completely lost. A sample .py file would be greatly appreciated!

Joseph DiLallo (DFA API Team)

unread,
Oct 23, 2013, 11:58:03 AM10/23/13
to google-doubleclick-...@googlegroups.com
Hey there,

We don't have a standard sample set, but this is something I've done a few times. I recommend you check out the Python client library's documentation and read up on how to generate, store, and retrieve OAuth 2.0 credentials. I usually do it like this:

from apiclient.discovery import build
import httplib2

def main():
  profile_id = 1234567  # Enter DFA profile ID here
  report_id = 2345678  # Enter report ID here
  file_id = 3456789  # Enter file ID here

  credentials = ...  # Get OAuth 2.0 credentials, probably from the client library's Storage.
  http = httplib2.Http()
  http = credentials.authorize(http)
  service = build('dfareporting', 'v1.3', http=http)

  download_report(service, profile_id, report_id, file_id, http)

def download_report(service, profile_id, report_id, file_id, http):
  report_file = service.reports().files().get(profileId=profile_id, reportId=report_id, fileId=file_id).execute()
  download_url = report_file['urls']['apiUrl']

  response, content = http.request(download_url)
  if response['status'] == '200':
    print content


I hope this helps you to get started!

Cheers,
- Joseph DiLallo, the DFA API Team

Simon Austin

unread,
Mar 13, 2014, 2:24:58 PM3/13/14
to google-doubleclick-...@googlegroups.com
Joseph,

To download a report, shouldn't the report_file = ... line be:
report_file = service.files().get(fileId=file_id,reportId=report_id).execute()

That is how I read this documentation: https://developers.google.com/doubleclick-advertisers/reporting/v1.3/files/get

Jonathon Imperiosi (DFA API Team)

unread,
Mar 13, 2014, 3:59:03 PM3/13/14
to google-doubleclick-...@googlegroups.com
Hi Simon,

Not sure if you've already seen, but we have a standard set of Python samples available now. The download example actually uses the Files service, as you mention.

To answer your question, there are two operations that allow you to download files:


These get operations provide basically the same functionally, the only real difference being that Reports.files.get requires an additional parameter to be passed (userProfileId). Knowing this, you can basically use the two interchangeably.

Regards,
- Jonathon Imperiosi, DFA API Team
Reply all
Reply to author
Forward
0 new messages