Note that if there is no currently logged-in user, #getUserId(HttpServletRequest) will
throw a NullPointerException. Example to require login for all pages:
publicclassServletSampleextendsAbstractAppEngineAuthorizationCodeServlet{@OverrideprotectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{// do stuff}@OverrideprotectedStringgetRedirectUri(HttpServletRequestreq)throwsServletException,IOException{GenericUrlurl=newGenericUrl(req.getRequestURL().toString());url.setRawPath("/oauth2callback");returnurl.build();}@OverrideprotectedAuthorizationCodeFlowinitializeFlow()throwsIOException{returnnewAuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),newUrlFetchTransport(),newGsonFactory(),newGenericUrl("https://server.example.com/token"),newBasicAuthentication("s6BhdRkqt3","7Fjfp0ZBr1KtDRbnfVdmIw"),"s6BhdRkqt3","https://server.example.com/authorize").setCredentialStore(newAppEngineCredentialStore()).build();}}
Returns the user ID for the given HTTP servlet request. This identifies your application's user
and is used to fetch persisted credentials for that user. Most commonly, this will be a user id
stored in the session or even the session id itself.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[],[]]