ThreeLeggedFlow oauthFlow = newFlow(userId);
oauthFlow.setJsonFactory(getJsonFactory());
oauthFlow.setHttpTransport(getHttpTransport());
try {
Credential cred = oauthFlow.loadCredential(pm);
if (cred != null && cred.isInvalid()) {
pm.deletePersistent(cred);
cred = null;
}
if (cred != null) {
req.setAttribute(AUTH_CREDENTIAL, cred);
try {
// Invoke the user code
super.service(req, resp);
} catch (HttpResponseException e) {
// Determine if we failed due to auth, or just failed
if (cred.isInvalid()) {
pm.deletePersistent(cred);
startAuthFlow(resp, pm, oauthFlow);
} else {
throw e;
}