0
votes

I downloaded the Adwords API client library for DOTNET. I am trying to use the GetAccountHierarchy.cs file example to get the account client list from Adwords Account. The code is appended as below:

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;
      }
    }
  }
}

I am trying to figure out where i can pass the parameter like my account username, password, developer token and access token to this function? Does anybody know how i can pass those info to this code? Thanks.

2

2 Answers

0
votes

It seems to me, wildly guessing, the account, username and password are present in some secondary resource files not passed in as arguments, or typed into the program as input, but loaded automatically from some 'special' location by the Google API.

Do you, perhaps, have a PGP type keys downloaded from the AdWords program?

0
votes

I know this getting old but you can pass parameter in your app.config/web.config

<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"/>

all oauth2 value you can get from google API Manager