OfflineCredentials generateCredential() limits and best practices

35 views
Skip to first unread message

Prashant Pandey

unread,
May 24, 2018, 2:07:08 AM5/24/18
to AdWords API and Google Ads API Forum

Here its mentioned, 

"The client libraries make sharing a credential across threads straightforward. Each client library has a session (or user) object with a credential that it reuses throughout its lifetime. To share the credential across threads, simply construct each session using the same credential. In all client libraries, the credential is a thread-safe object that refreshes itself synchronously when its access token expires."


My use case - Using this I am uploading emails/phone numbers to my customers adWords account.
I am using the following code to generate credentials 
Credential oAuth2Credential = new OfflineCredentials.Builder()
 
.forApi(OfflineCredentials.Api.ADWORDS)
 
.withClientSecrets(GOOGLE_ADWORDS_CLIENT_ID, GOOGLE_ADWORDS_CLIENT_SECRET)
 
.withRefreshToken(refreshToken).build().generateCredential();

// Construct an AdWordsSession.
AdWordsSession session = new AdWordsSession.Builder().withClientCustomerId(clientCustomerId)
 
.withDeveloperToken(GOOGLE_ADWORDS_DEVELOPER_TOKEN)
 
.withOAuth2Credential(oAuth2Credential)
 
.withUserAgent("test_user").build();

AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
userListService
= adWordsServices.get(session, AdwordsUserListServiceInterface.class);

I am batching 20k emails from my end and for every 20k emails I am firing the above code. But according to the above quote
(which is a bit confusing), this is not a best practice? If I understand the above paragraph correctly, do you guys suggest that the 
oAuth2Credential variable be made into Thread-Safe object and then it should be used to create session persistently? 

Or If I am mistaken can you guys please help me in explaining this?

Thanks

Peter Oliquino (AdWords API Team)

unread,
May 24, 2018, 5:01:40 AM5/24/18
to AdWords API and Google Ads API Forum
 Hi Prashant,

What the paragraph implies is that each request you make should make use of your already thread-safe credentials by simply instantiating the sessions object. In the examples in the client library such as this one, you no longer need to modify the current implementation as it already automatically does this for you. It retrieves your credentials from your properties file and refreshes your accessTokens for each call that you make.

Best regards,
Peter
AdWords API Team

Prashant Pandey

unread,
May 24, 2018, 5:15:20 AM5/24/18
to adwordsapiadv...@google.com, AdWords API and Google Ads API Forum
Hi Peter, 
Thanks for the reply. I am confused by your reply. If I am making 10000 request (with 2000 emails each) and I am calling the above snippet 10000 times , generateCredential() will be called 10000 times.  I saw the code and it makes HTTP calls.

In the examples in the client library such as this one, you no longer need to modify the current implementation as it already automatically does this for you. It retrieves your credentials from your properties file and refreshes your accessTokens for each call that you make.
What exactly do you mean by properties file? Do you mean that after the first call to generateCredential() you are storing it in some cache and retrieving it (to instantiate the session object) for all subsequent calls?  
And also it will only refresh for the access token only after an hour correct?

Please bear with me. Thanks

--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
 
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to adwor...@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "AdWords API and Google Ads API Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/adwords-api/-ZTdzU32paI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to adwords-api+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-api/7c89fb07-650f-4599-b17c-9f634ce431b3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages