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
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
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 ----------
AdWordsUser user = serviceUsers.get(stub._getService());
will have to be rewritten to use that public method in the future.