简体   繁体   中英

How to get an adwords account client customer id to link it to a MCC account?

I'd like to add an Adwords account to a MCC account via Google Adwords API on my web application. I guess I just need to add a ManagedCustomerLink

The thing is I don't know how to get clientCustomerId . I thought by authenticating the user via OAuth2 on my application with the matching scope, I could somehow get their clientCustomerId but I could not find it.

Thank you by advance for your help !

You can get the client customer id by running the next code:

CustomerServiceInterface customerService = adWordsServices.get(session, CustomerServiceInterface.class);
Customer[] customers;
try {
    customers = customerService.getCustomers();
    for (Customer customer : customers) {
        Long customerId = customer.getCustomerId();
        System.out.println(customerId);
    }
} catch (RemoteException e) {
    e.printStackTrace();
}

In order to get the user session you have to use Oauth 2.0 and ask for his credentials.

Based on reading your exchange with George Astonishing above, I wonder if you really want to add AdWords accounts to your MCC.

There is a difference between an AdWords account owner

  1. Authorizing your app to access their data, vs
  2. Becoming your company's client for full AdWords management.

If this AdWords account belongs to your company's client, and you are taking over their account management, then you can use ManagedCustomerService to add the account to your MCC. You can get the account's 10-digit AdWords Customer ID using CustomerService .

On the other hand, if this is for a web app, and you just want users to be able to authorize with OAuth2, then you should follow this guide instead.

Here, visit this link to Google Adwords library,

https://github.com/googleads/googleads-php-lib/blob/master/examples/AdWords/v201509/AccountManagement/GetAccountHierarchy.php

Once you create a new user

   $user = new AdWordsUser(); // with your API creds using OAUTH       
   GetAccountHierarchyExample(AdWordsUser $user)  //found in link

Just run the function. It will list all your managed subaccounts.

Here is the code you can get your client customers

public Customer[] GetAllManagerClientsList(string authorizationCode)
        {
            string baseURL = _configuration.GetValue<string>("URL:SiteURL");
            AdsOAuthProviderForApplications oAuth2Provider = (user.OAuthProvider as AdsOAuthProviderForApplications);
            oAuth2Provider.Config.OAuth2RedirectUri = baseURL + "/google-auth-callback";

            oAuth2Provider.FetchAccessAndRefreshTokens(authorizationCode);

            //Get customerID
            user.Config.OAuth2AccessToken = oAuth2Provider.Config.OAuth2AccessToken;
            user.Config.OAuth2RefreshToken = oAuth2Provider.Config.OAuth2RefreshToken;
            //store in cache
            var chechedEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(5));


            CustomerService customerService = (CustomerService)user.GetService(AdWordsService.v201809.CustomerService);
            var customersList = customerService.getCustomers();

            var ClientCustomers = customersList != null && customersList.Length > 0 ? customersList.Where(c => c.canManageClients == false).ToList() : null;
            if (ClientCustomers.Count() > 0)
            {
                return ClientCustomers.ToArray();
            }
            else
            {
                return null;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM