Google Adwords API C#客户端库

Google Adwords API C#客户端库,c#,google-ads-api,C#,Google Ads Api,我下载了用于DOTNET的Adwords API客户端库。我试图使用GetAccountHierarchy.cs文件示例从Adwords account获取帐户客户端列表。代码附加如下: using Google.Api.Ads.AdWords.Lib; using Google.Api.Ads.AdWords.v201209; using System; using System.Collections.Generic; using System.IO; using System.Text;

我下载了用于DOTNET的Adwords API客户端库。我试图使用GetAccountHierarchy.cs文件示例从Adwords account获取帐户客户端列表。代码附加如下:

using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201209;

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Google.Api.Ads.AdWords.Examples.CSharp.v201209 {
  /// <summary>
  /// This code example illustrates how to retrieve the account hierarchy under
  /// an account. This code example won't work with Test Accounts. See
  /// https://developers.google.com/adwords/api/docs/test-accounts
  ///
  /// Tags: ManagedCustomerService.get
  /// </summary>
  public class GetAccountHierarchy : ExampleBase {
    /// <summary>
    /// Main method, to run this code example as a standalone application.
    /// </summary>
    /// <param name="args">The command line arguments.</param>
    public static void Main(string[] args) {
      GetAccountHierarchy codeExample = new GetAccountHierarchy();
      Console.WriteLine(codeExample.Description);
      try {
        codeExample.Run(new AdWordsUser());
      } catch (Exception ex) {
        Console.WriteLine("An exception occurred while running this code example. {0}",
            ExampleUtilities.FormatException(ex));
      }
    }

    /// <summary>
    /// Returns a description about the code example.
    /// </summary>
    public override string Description {
      get {
        return "This code example illustrates how to retrieve the account hierarchy under" +
            " an account. This code example won't work with Test Accounts. See " +
            "https://developers.google.com/adwords/api/docs/test-accounts";
      }
    }

    /// <summary>
    /// Runs the code example.
    /// </summary>
    /// <param name="user">The AdWords user.</param>
    public void Run(AdWordsUser user) {
      // Get the ManagedCustomerService.
      ManagedCustomerService managedCustomerService = (ManagedCustomerService) user.GetService(
          AdWordsService.v201209.ManagedCustomerService);
      managedCustomerService.RequestHeader.clientCustomerId = null;

      // Create selector.
      Selector selector = new Selector();
      selector.fields = new String[] {"Login", "CustomerId", "Name"};

      try {
        // Get results.
        ManagedCustomerPage page = managedCustomerService.get(selector);

        // Display serviced account graph.
        if (page.entries != null) {
          // Create map from customerId to customer node.
          Dictionary<long, ManagedCustomerTreeNode> customerIdToCustomerNode =
              new Dictionary<long, ManagedCustomerTreeNode>();

          // Create account tree nodes for each customer.
          foreach (ManagedCustomer customer in page.entries) {
            ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
            node.Account = customer;
            customerIdToCustomerNode.Add(customer.customerId, node);
          }

          // For each link, connect nodes in tree.
          if (page.links != null) {
            foreach (ManagedCustomerLink link in page.links) {
              ManagedCustomerTreeNode managerNode =
                  customerIdToCustomerNode[link.managerCustomerId];
              ManagedCustomerTreeNode childNode = customerIdToCustomerNode[link.clientCustomerId];
              childNode.ParentNode = managerNode;
              if (managerNode != null) {
                managerNode.ChildAccounts.Add(childNode);
              }
            }
          }

          // Find the root account node in the tree.
          ManagedCustomerTreeNode rootNode = null;
          foreach (ManagedCustomer account in page.entries) {
            if (customerIdToCustomerNode[account.customerId].ParentNode == null) {
              rootNode = customerIdToCustomerNode[account.customerId];
              break;
            }
          }

          // Display account tree.
          Console.WriteLine("Login, CustomerId, Name");
          Console.WriteLine(rootNode.ToTreeString(0, new StringBuilder()));
        } else {
          Console.WriteLine("No serviced accounts were found.");
        }
      } catch (Exception ex) {
        throw new System.ApplicationException("Failed to create ad groups.", ex);
      }
    }

    /**
     * Example implementation of a node that would exist in an account tree.
     */
    class ManagedCustomerTreeNode {
      /// <summary>
      /// The parent node.
      /// </summary>
      private ManagedCustomerTreeNode parentNode;

      /// <summary>
      /// The account associated with this node.
      /// </summary>
      private ManagedCustomer account;

      /// <summary>
      /// The list of child accounts.
      /// </summary>
      private List<ManagedCustomerTreeNode> childAccounts = new List<ManagedCustomerTreeNode>();

      /// <summary>
      /// Gets or sets the parent node.
      /// </summary>
      public ManagedCustomerTreeNode ParentNode {
        get { return parentNode; }
        set { parentNode = value; }
      }

      /// <summary>
      /// Gets or sets the account.
      /// </summary>
      public ManagedCustomer Account {
        get { return account; }
        set { account = value; }
      }

      /// <summary>
      /// Gets the child accounts.
      /// </summary>
      public List<ManagedCustomerTreeNode> ChildAccounts {
        get { return childAccounts; }
      }

      /// <summary>
      /// Returns a <see cref="System.String"/> that represents this instance.
      /// </summary>
      /// <returns>
      /// A <see cref="System.String"/> that represents this instance.
      /// </returns>
      public override String ToString() {
        String login = String.IsNullOrEmpty(account.login) ? "(no login)" : account.login;
        return String.Format("{0}, {1}, {2}", login, account.customerId, account.name);
      }

      /// <summary>
      /// Returns a string representation of the current level of the tree and
      /// recursively returns the string representation of the levels below it.
      /// </summary>
      /// <param name="depth">The depth of the node.</param>
      /// <param name="sb">The String Builder containing the tree
      /// representation.</param>
      /// <returns>The tree string representation.</returns>
      public StringBuilder ToTreeString(int depth, StringBuilder sb) {
        sb.Append(new String('-', depth * 2));
        sb.Append(this);
        sb.Append("\n");
        foreach (ManagedCustomerTreeNode childAccount in childAccounts) {
          childAccount.ToTreeString(depth + 1, sb);
        }
        return sb;
      }
    }
  }
}
使用Google.Api.Ads.AdWords.Lib;
使用Google.Api.Ads.AdWords.v201209;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用系统文本;
名称空间Google.Api.Ads.AdWords.Examples.CSharp.v201209{
/// 
///此代码示例说明了如何在下检索帐户层次结构
///帐户。此代码示例不适用于测试帐户。请参阅
/// https://developers.google.com/adwords/api/docs/test-accounts
///
///标记:ManagedCustomerService.get
/// 
公共类GetAccountHierarchy:ExampleBase{
/// 
///Main方法,将此代码示例作为独立应用程序运行。
/// 
///命令行参数。
公共静态void Main(字符串[]args){
GetAccountHierarchy codeExample=新建GetAccountHierarchy();
Console.WriteLine(codeExample.Description);
试一试{
运行(新的AdWordsUser());
}捕获(例外情况除外){
WriteLine(“运行此代码示例时发生异常。{0}”,
例如Utilities.FormatException(ex));
}
}
/// 
///返回有关代码示例的说明。
/// 
公共重写字符串描述{
得到{
return“此代码示例演示了如何检索下的帐户层次结构”+
“帐户。此代码示例不适用于测试帐户。请参阅”+
"https://developers.google.com/adwords/api/docs/test-accounts";
}
}
/// 
///运行代码示例。
/// 
///AdWords用户。
公共作废运行(AdWordsUser用户){
//获取ManagedCustomerService。
ManagedCustomerService ManagedCustomerService=(ManagedCustomerService)user.GetService(
AdWordsService.v201209.ManagedCustomerService);
managedCustomerService.RequestHeader.clientCustomerId=null;
//创建选择器。
选择器=新选择器();
selector.fields=新字符串[]{“Login”、“CustomerId”、“Name”};
试一试{
//得到结果。
ManagedCustomerPage=managedCustomerService.get(选择器);
//显示已服务帐户图。
如果(page.entries!=null){
//创建从customerId到customer节点的映射。
字典customerIdToCustomerNode=
新字典();
//为每个客户创建帐户树节点。
foreach(第页条目中的ManagedCustomer客户){
ManagedCustomerTreeNode=新ManagedCustomerTreeNode();
节点.账户=客户;
customerIdToCustomerNode.Add(customer.customerId,节点);
}
//对于每个链接,连接树中的节点。
如果(page.links!=null){
foreach(ManagedCustomerLink页中的链接.links){
ManagedCustomerTreeNode管理节点=
customerIdToCustomerNode[link.managerCustomerId];
ManagedCustomerTreeNode childNode=customerIdToCustomerNode[link.clientCustomerId];
childNode.ParentNode=managerNode;
if(managerNode!=null){
managerNode.ChildAccounts.Add(childNode);
}
}
}
//在树中查找根帐户节点。
ManagedCustomerTreeNode rootNode=null;
foreach(第页条目中的ManagedCustomer帐户){
if(customerIdToCustomerNode[account.customerId].ParentNode==null){
rootNode=customerIdToCustomerNode[account.customerId];
打破
}
}
//显示帐户树。
Console.WriteLine(“登录名、客户ID、名称”);
Console.WriteLine(rootNode.totreString(0,new StringBuilder());
}否则{
Console.WriteLine(“未找到已服务的帐户”);
}
}捕获(例外情况除外){
抛出新System.ApplicationException(“未能创建广告组。”,例如);
}
}
/**
*存在于帐户树中的节点的示例实现。
*/
类ManagedCustomerTreeNode{
/// 
///父节点。
/// 
私有ManagedCustomerTreeNode父节点;
/// 
///与此节点关联的帐户。
/// 
私人管理客户账户;
/// 
///子帐户的列表。
/// 
private List childAccounts=新列表();
/// 
///获取或设置父节点。
/// 
公共管理的CustomerTreeNode父节点{
获取{return parentNode;}
设置{parentNode=value;}
}
/// 
///获取或设置帐户。
/// 
公共管理客户帐户{
获取{返回帐户;}
设置{account=value;}
}
/// 
///获取子帐户。
/// 
公众帐户清单{
获取{return childAccounts;}
}
/// 
///返回表示此实例的。
/// 
/// 
///表示此实例的。
/// 
公共重写字符串ToString(){
String login=String.IsNullOrEmpty(account.login)?“(无登录)”:account.login;
返回String.Format(“{0},{1},{2}”,login,account.customerId,account.name);
}
/// 
///返回树的当前级别的字符串表示形式,并
///递归返回它下面级别的字符串表示形式。
/// 
///节点的深度。
///包含树的字符串生成器
///代表性。
///树字符串表示法。
公共StringBuilder到重建字符串(整型深度,StringBuilder sb){
sb.Append(新字符串('-',深度*2));
某人附加(这个);
某人附加(“\n”);
<add key="ClientCustomerId" value="insert your client customer id"/>
<add key="AuthorizationMethod" value="OAuth2"/>
<add key="OAuth2ClientId" value="insert oauth2 client id"/>
<add key="OAuth2ClientSecret" value="insert oauth client scret"/>
<add key="OAuth2RefreshToken" value="insert oauth2 refresh token"/>