Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (c) 2012 Google Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  * in compliance with the License. You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.api.client.extensions.appengine.auth.oauth2;
import  com.google.appengine.api.users.UserServiceFactory;
Simple extension of AbstractAuthorizationCodeServlet that uses the currently logged-in Google Account user, as directed in Security and Authentication.

Note that if there is no currently logged-in user, getUserId(HttpServletRequest) will throw a NullPointerException. Example to require login for all pages:

<security-constraint>
<web-resource-collection>
<web-resource-name>any</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
 

Sample usage:

public class ServletSample extends AbstractAppEngineAuthorizationCodeServlet {

Author(s):
Yaniv Inbar
Since:
1.7
Override:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { // do stuff }
Override:
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException { GenericUrl url = new GenericUrl(req.getRequestURL().toString()); url.setRawPath("/oauth2callback"); return url.build(); }
Override:
protected AuthorizationCodeFlow initializeFlow() throws IOException { return new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(), new UrlFetchTransport(), new JacksonFactory(), new GenericUrl("https://server.example.com/token"), new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"), "s6BhdRkqt3", "https://server.example.com/authorize").setCredentialStore(new AppEngineCredentialStore()) .build(); } }
  private static final long serialVersionUID = 1L;
    return UserServiceFactory.getUserService().getCurrentUser().getUserId();
  }
New to GrepCode? Check out our FAQ X