Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ElvisLives/1287940 to your computer and use it in GitHub Desktop.
Save ElvisLives/1287940 to your computer and use it in GitHub Desktop.
An Example of calling the AdHoc reporting of the Google Adwords API v201109
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
/// <summary>
/// Here is an example of calling the AdHoc reporting of the Google Adwords API v201109
/// </summary>
public void GoogleAdwordsv201109_Adhoc_reports_CSharp_Example()
{
string URL = "https://adwords.google.com/api/adwords/reportdownload/v201109";
string authToken = "your_auth_token";
string clientId = "your_client_id";
string fileName = "your_file_name";
var request = WebRequest.Create(URL) as HttpWebRequest;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Headers.Add("Authorization",
string.Format("GoogleLogin auth={0}", authToken));
request.Headers.Add("clientCustomerId", clientId);
string xml =
@"<reportDefinition>
<selector>
<fields>Date</fields>
<fields>CampaignId</fields>
<fields>Id</fields>
<fields>HourOfDay</fields>
<fields>Impressions</fields>
<fields>Clicks</fields>
<fields>Cost</fields>
<dateRange>
<min>20111001</min>
<max>20111031</max>
</dateRange>
</selector>
<reportName>Test Report</reportName>
<reportType>ADGROUP_PERFORMANCE_REPORT</reportType>
<dateRangeType>CUSTOM_DATE</dateRangeType>
<downloadFormat>CSV</downloadFormat>
</reportDefinition>";
using (var sw = new StreamWriter(request.GetRequestStream()))
{
sw.Write("__rdxml=" + HttpUtility.UrlEncode(xml));
}
using (var httpWebResponse = request.GetResponse() as HttpWebResponse)
{
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = httpWebResponse.GetResponseStream())
{
using (FileStream fileStream = File.Create(string.Format("{0}.csv", fileName)))
{
stream.CopyTo(fileStream);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment