Auth Token Expiry

66 views
Skip to first unread message

shriny

unread,
Jan 28, 2010, 4:50:26 PM1/28/10
to AdWords API Forum
How often will the auth token expire?

How to handle it in client side, should we store it in a database and
use it or get a new token everytime we do any adwords operations ?

This would be helpful in designing our systems.


-Shriny


nothize

unread,
Jan 29, 2010, 3:20:34 AM1/29/10
to AdWords API Forum
Hi Shriny,

I've read that it's about two weeks but could be vary from time to
time. Also, a password change may invalidate the token for a short
period, say 10 minutes.

I'd prefer to refresh the token whenever it's invalid. AOP techniques
(AspectJ) should be a perfect match. Then the caching and refreshing
mechanism can be applied to the whole application transparently.

Otherwise, you may have to tweak the API library you're using or do
regular health checking or token refurbishment to reduce the out of
service duration.

HTH
N

nothize

unread,
Feb 1, 2010, 7:33:05 AM2/1/10
to AdWords API Forum
Here is some hints for the AOP caching mechanism using Java 1.5+ with
AspectJ.

1. Wraps all API calls that might throw the ApiException.
2. Look for the GOOGLE_ACCOUNT_COOKIE_INVALID exception reason.
3. Reverse lookup AdWordsUser from Stub._getService() for the email
and pwd.
4. Request a new auth token and update the cache.
5. Update the auth token back to the user instance and Soap header.
6. Retry the last operation.

The use of call() instead of execute() is because I'm using compile-
time weaving instead of load-time weaving to avoid any changes to the
deployment target's environment or the AdWords library itself.

-------- begin --------

@Pointcut("call(public * com.google.api.adwords..*(..) throws
com.google.api.adwords.v200909.cm.ApiException) && target(stub)")
void authWrapperCut(Stub stub) {}

@Around("authWrapperCut(stub)")
public Object authWrapper(ProceedingJoinPoint pjp, Stub stub) throws
Throwable {
try {
return pjp.proceed();
} catch (Throwable e) {
if ( e instanceof ApiException ) {
ApiException ae = (ApiException) e;
ApiError[] errors = ae.getErrors();
if ( null != errors && errors.length > 0 ) {
if ( errors[0] instanceof AuthenticationError ) {
// Renew the auth token when this authentication error happened.
if
( AuthenticationErrorReason.GOOGLE_ACCOUNT_COOKIE_INVALID.equals
(((AuthenticationError)errors[0]).getReason())) {
AdWordsUser user = serviceUsers.get(stub._getService());
String token = requestAuthToken(user.getEmail(),
user.getPassword());
user.setAuthToken(token);

SOAPHeaderElement he = stub.getHeaders()[0];
SoapHeader sh = (SoapHeader) he.getObjectValue();
sh.setAuthToken(token);

return pjp.proceed();
}
}
}
}
throw e;
}
}

-------- end ----------

nothize

unread,
Feb 1, 2010, 8:27:43 PM2/1/10
to AdWords API Forum
Note that the next AdWords Java API milestone 7.0.0 will have a public
method available for getting values from serviceUsers.

AdWordsUser user = serviceUsers.get(stub._getService());

will have to be rewritten to use that public method in the future.

Reply all
Reply to author
Forward
0 new messages