Reporting Request

52 views
Skip to first unread message

Demetris Lappas

unread,
Aug 13, 2015, 8:59:41 AM8/13/15
to AdWords API Forum
I am trying to build an application for internal business use that will pull Adwords Custom Reports automatically.

I've attained a developer's token, and an access code which I refresh every hour. I've also written a code in VB.Net (translated from C# code) to make a request and get a response. However I get an error every time I try to get a response from my request. Below is the VB Code, and below that is the XML in TextBox1.Text for the report I want to attain.

(I've commented on where the error occurs)
VB Code:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim reportdefinitionxml As String = TextBox1.Text
        Dim accesstoken As String = "XXXXXXXXX"
        Dim clientcustomerid As String = "XXX-XXX-XXXX"
        Dim developertoken As String = "XXXXXXXXXX"
        Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\adwordsapi.txt"
        Dim version As String = "v201506"

        Dim downloadurl As Uri = New Uri("https://adwords.google.com/api/adwords/reportdownload/" & version)

        Dim request As WebRequest = HttpWebRequest.Create(downloadurl)
        request.Headers.Add("clientCustomerId: " & clientcustomerid)
        request.Headers.Add("developerToken: " & developertoken)
        request.Headers.Add("Authorization: Bearer " & accesstoken)

        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        Using writer As System.IO.StreamWriter = New System.IO.StreamWriter(request.GetRequestStream)
            writer.Write("__rdxml=" & System.Web.HttpUtility.UrlEncode(reportdefinitionxml))
        End Using

        Dim response As WebResponse = Nothing
        Dim success As Boolean = False
        Try
            response = request.GetResponse 'This is where an error occurs'
            success = True
        Catch ex As WebException 'The error exception is {"The remote server returned an error: (400) Bad Request."}'
            response = ex.Response
        End Try

        DownloadReportToFile(filePath, response)

    End Sub



XML in Textbox1.Text:

  <selector>
    <fields>CampaignId</fields>
    <fields>Id</fields>
    <fields>Impressions</fields>
    <fields>Clicks</fields>
    <fields>Cost</fields>
    <predicates>
      <field>Status</field>
      <operator>IN</operator>
      <values>ENABLED</values>
      <values>PAUSED</values>
    </predicates>
  </selector>
  <reportName>Custom Adgroup Performance Report</reportName>
  <reportType>ADGROUP_PERFORMANCE_REPORT</reportType>
  <dateRangeType>LAST_7_DAYS</dateRangeType>
  <downloadFormat>CSV</downloadFormat>
</reportDefinition>



If anybody could point out where I am going wrong that would be most appreciated.

Thanks!


Josh Radcliff (AdWords API Team)

unread,
Aug 13, 2015, 3:10:45 PM8/13/15
to AdWords API Forum
Hi,

Have you considered using the .NET client library? It will take care of the HTTP layer for you (among other things) and has a number of convenient utilities for running reports.

Thanks,
Josh, AdWords API Team
Reply all
Reply to author
Forward
0 new messages