Getting User ID of authenticated user.

594 views
Skip to first unread message

john p

unread,
Jan 3, 2021, 11:02:10 AM1/3/21
to AdWords API and Google Ads API Forum
Using Google Ads API v6 GoogleAdsService.Search in .NET:

I can retrieve a list of users (CustomerUserAccess resource) with access to the given account. The list includes email address, Google Ads user id and access role for each row.

select customer_user_access.user_id, customer_user_access.email_address, customer_user_access.access_role from customer_user_access

I'm aiming to filter these results in the query to the row for the current OAuth authenticated user e.g:

select customer_user_access.user_id, customer_user_access.email_address, customer_user_access.access_role from customer_user_access where customer_user_access.user_id = '<current user id>'.

Is there a way to retrieve the ads user id and / or email of the logged in user? The UserCredential object has null for the UserId property.

Thanks


Google Ads API Forum Advisor Prod

unread,
Jan 4, 2021, 1:32:15 AM1/4/21
to john...@gmail.com, adwor...@googlegroups.com
Hi John,

Thank you for reaching out.

If my understanding is correct that you wish to retrieve the email address and user ID from the credentials object, then I'm afraid this is not possible.

The UserCredential object's details may not be available for users since it is not listed as one of the supported resources in the documentation (ex. v6 > resources).

Best regards,

Google Logo
Peter Laurence Napa Oliquino
Google Ads API Team
 


ref:_00D1U1174p._5004Q2AGyWB:ref

john p

unread,
Jan 4, 2021, 4:20:13 PM1/4/21
to AdWords API and Google Ads API Forum
Thanks Peter

I've managed to get the email address by decoding the IDToken property of the UserCredential as a JwtSecurityToken. This contains the email address as a claim.

Google Ads API Forum Advisor Prod

unread,
Jan 5, 2021, 11:19:52 AM1/5/21
to john...@gmail.com, adwor...@googlegroups.com
Hi John,

The UserID field in the credentials object is unrelated to the UserId field in Ads. 

One way you can try to solve this problem is as follows:

1. Use the people.get method from the People API to get the login email of the current user: https://developers.google.com/people/api/rest/v1/people/get

2. Next, use the email address to filter on the query.

Note that the people.get() method will give you a person_id, but this will be different from the user ID. The User ID that is exposed by Google Ads API is specific to Google Ads API and not something unique across other Google systems.

Cheers
Anash

ref:_00D1U1174p._5004Q2AGyWB:ref

john p

unread,
Jan 5, 2021, 3:19:22 PM1/5/21
to AdWords API and Google Ads API Forum
Thanks for the further info Anash. As detailed in response above, I have retrieved the email address using IDToken of the UserCredential.

Avinash Pawar

unread,
May 11, 2021, 4:37:38 PM5/11/21
to AdWords API and Google Ads API Forum
Hi John,

I am new to Google Ads API 

I have had success running get_account_hierarchy.cs (the documentation code example).   Please guide me on how you get to work with CustomerUserAccess resource.  

Google Ads API Forum Advisor Prod

unread,
May 11, 2021, 11:02:37 PM5/11/21
to avmv...@gmail.com, adwor...@googlegroups.com
Hi Avinash,


Thank you for reaching out.

You can check out this guide for more information on how you can manage your customer's user access. If you have additional clarifications, let us know and we'll be happy to assist you further.


Best regards,

Google Logo
Peter Laurence Napa Oliquino
Google Ads API Team
 


ref:_00D1U1174p._5004Q2HAA3w:ref

Avinash Pawar

unread,
May 13, 2021, 8:19:54 AM5/13/21
to AdWords API and Google Ads API Forum
Dear Support Team,

It would be of great assistance to the entire group if your give us sample code to get the following info

I can retrieve a list of users (CustomerUserAccess resource) with access to the given account. The list includes email address, Google Ads user id and access role for each row.

select customer_user_access.user_id, customer_user_access.email_address, customer_user_access.access_role from customer_user_access;

With limited knowledge I can run get get_account_hierarchy.cs and obtain results thanks to the documentation.   If you can provide a sample code to get user info that will be of great assistance to the entire user community.

Best Regards

Google Ads API Forum Advisor Prod

unread,
May 13, 2021, 3:22:37 PM5/13/21
to avmv...@gmail.com, adwor...@googlegroups.com

Hi Avinash,

Thank you for reaching out. This actually could be achieved by simply replacing the query in this code and fetching the needed user info from the customer_user_access.

Thanks and regards,
Xiaoming, Google Ads API Team
 


Google Logo
Xiaoming
Google Ads API Team
 


ref:_00D1U1174p._5004Q2HAA3w:ref

Avinash Pawar

unread,
May 16, 2021, 10:16:50 AM5/16/21
to AdWords API and Google Ads API Forum
Hi Xiaoming,

Thanks for your assistance, I was able extract user info.   For other developers on the forum here is some code to save a whole lot of research [from a developer who relies on low code solutions :-)].  Please note I used the python code (get_keyword_stats.py)  in the developers.google.com/google-ads

def main(client, customer_id=Null):
    ga_service = client.get_service("GoogleAdsService")
    
    query = """
        SELECT customer_user_access.user_id, customer_user_access.resource_name, customer_user_access.inviter_user_email_address, customer_user_access.email_address, customer_user_access.access_role, customer_user_access.access_creation_date_time, customer.id, customer.manager, customer.descriptive_name, customer.currency_code FROM customer_user_access"""

    # Issues a search request using streaming.
    customer_id='xxxxxxxxxx'
    search_request = client.get_type("SearchGoogleAdsStreamRequest")
    search_request.customer_id = customer_id
    search_request.query = query
    response = ga_service.search_stream(search_request)
 
    #used below loop to find the attributes in the response object
    #for att in dir(response):
      #print (att, getattr(response,att))
    print(response._stored_first_result)  

Cheers


Reply all
Reply to author
Forward
0 new messages