Google Adwords API调用通过C#下载数据

时间:2012-12-06 22:49:30

标签: c# google-adwords

我正在开展一些C#项目,以便对 Adwords 进行API调用以下载数据。

我已经使用以下方式获取访问令牌:

WebRequest webRequest = HttpWebRequest.Create("https://www.google.com/accounts/ClientLogin");
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        string postParams =

            "accountType=" + HttpUtility.UrlEncode("GOOGLE") +
            "&Email=" + HttpUtility.UrlEncode("xxxx@xxxx.com") +
            "&Passwd=" + HttpUtility.UrlEncode("xxxx") +
            "&service=" + HttpUtility.UrlEncode("adwords") +
            "&source=" + HttpUtility.UrlEncode(string.Format("{0}-{1}-{2}", "sample1",
                "sample2", "1"));

        byte[] postBytes = Encoding.UTF8.GetBytes(postParams);
        webRequest.ContentLength = postBytes.Length;

        using (Stream strmReq = webRequest.GetRequestStream())
        {
            strmReq.Write(postBytes, 0, postBytes.Length);
        }

        string retVal = "";
        try
        {
            WebResponse response = webRequest.GetResponse();

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string sResponse = reader.ReadToEnd();
                string[] splits = sResponse.Split('\n');
                foreach (string split in splits)
                {
                    string[] subsplits = split.Split('=');
                    if (subsplits.Length >= 2 && subsplits[0] == "Auth")
                    {
                        retVal = subsplits[1];
                    }
                }
            }
        }
        catch (WebException ex)
        {
            throw new ApplicationException("Could not generate auth token.", ex);
        }

如果有人能让我知道下一步进行API调用以下载数据,我将不胜感激。 https://developers.google.com/adwords/api/docs/guides/reporting

感谢。

1 个答案:

答案 0 :(得分:0)

除了@Jon提到的有关使用客户端库的内容之外,您还应该查看下载的示例目录,特别是reporting example,以及XML Schema for the report definition来修改示例。具体要求。