[Google Ads API] How to set Oauth credentials using java client lib (google-ads-java)

696 views
Skip to first unread message

alok

unread,
Jun 20, 2019, 10:52:22 AM6/20/19
to AdWords API and Google Ads API Forum
Hi,

We are planing to use google-ads-java client library to migrate from Google Adwords API to Google Ads API.

I have all the required values of client_id,client_secret,refresh token and access token etc. Whats the best way to set Oauth credentials using java client.
I have already gone though test examples which creates GoogleAdsClient from properties file but this doesn't fit my requirement as we access different MCCs in same platform.

what are the best way to create credentials object (please be specific to the java class to be used) and use it with GoogleAdsClient.
GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder().setCredentials(theCredentials)

Thanks
Alok

Google Ads API Forum Advisor Prod

unread,
Jun 20, 2019, 3:58:03 PM6/20/19
to aloks...@gmail.com, adwor...@googlegroups.com
Hi Alok, 

You can construct the Credentials object as below and pass them to the GoogleAdsClient:
 
Credentials credentials =
    UserCredentials.newBuilder()
        .setClientId("INSERT_CLIENT_ID")
        .setClientSecret("INSERT_CLIENT_SECRET")
        .setRefreshToken("INSERT_REFRESH_TOKEN")
        .build();
GoogleAdsClient googleAdsClient =
    GoogleAdsClient.newBuilder()
        .setCredentials(credentials)
        .setDeveloperToken("INSERT_DEVELOPER_TOKEN_HERE")
        .setLoginCustomerId("INSERT_LOGIN_CUSTOMER_ID_HERE") // Manager accounts only.
        .build();
Please refer to this guide for more information. Let me know if you have any questions.

Thanks,
Bharani, Google Ads API Team

ref:_00D1U1174p._5001UCYbnu:ref

alok

unread,
Jun 21, 2019, 6:59:52 AM6/21/19
to AdWords API and Google Ads API Forum
 Hi Bharani,

 Thanks for the info.Another follow up question on the same.

  There is an option of setting access token in UserCredentials object but it is not being used as per the example given below by you. Is it not mandatory to set access token anymore and is access token automatically generated using cleint_id,client_secret and refresh token?? What happens if the access token expires after 60 minutes,Is that already handled too??

Thanks,
Alok

Google Ads API Forum Advisor Prod

unread,
Jun 21, 2019, 2:21:02 PM6/21/19
to aloks...@gmail.com, adwor...@googlegroups.com
Hello Alok, 

If you are using our client libraries, you will have to generate the refresh token only once and the access tokens will be automatically refreshed with the refresh token that you are using. Please refer to this guide for more information. 
Reply all
Reply to author
Forward
0 new messages