List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleAuthorizationCodeFlow GoogleAuthorizationCodeFlow
public GoogleAuthorizationCodeFlow(HttpTransport transport, JsonFactory jsonFactory, String clientId,
String clientSecret, Collection<String> scopes)
From source file:com.otway.picasasync.webclient.GoogleOAuth.java
License:Apache License
public PicasawebClient authenticatePicasa(Settings settings, boolean allowInteractive, SyncState state) throws IOException, GeneralSecurityException { final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); log.info("Preparing to authenticate via OAuth..."); Credential cred = null;// w w w. j a v a 2s . c o m String refreshToken = settings.getRefreshToken(); if (refreshToken != null) { // We have a refresh token - so get some refreshed credentials cred = getRefreshedCredentials(refreshToken); } if (cred == null && allowInteractive) { // Either there was no valid refresh token, or the credentials could not // be created (they may have been revoked). So run the auth flow log.info("No credentials - beginning OAuth flow..."); state.setStatus("Requesting Google Authentication..."); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(httpTransport, jsonFactory, clientId, clientSecret, Collections.singleton(scope)); String authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(redirectUrl) .setAccessType("offline").setApprovalPrompt("force").build(); try { OAuthGUI authGUI = new OAuthGUI(); // Display the interactive GUI for the user to log in via the browser String code = authGUI.initAndShowGUI(authorizationUrl, state); log.info("Token received from UI. Requesting credentials..."); // Now we have the code from the interactive login, set up the // credentials request and call it. GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUrl).execute(); // Retrieve the credential from the request response cred = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory) .setClientSecrets(clientId, clientSecret).build().setFromTokenResponse(response); state.setStatus("Google Authentication succeeded."); log.info("Credentials received - storing refresh token..."); // Squirrel this away for next time settings.setRefreshToken(cred.getRefreshToken()); settings.saveSettings(); } catch (Exception ex) { log.error("Failed to initialise interactive OAuth GUI", ex); } } if (cred != null) { log.info("Building PicasaWeb Client..."); // Build a web client using the credentials we created return new PicasawebClient(cred); } return null; }
From source file:net.wasdev.gameon.auth.google.GoogleAuth.java
License:Apache License
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JsonFactory jsonFactory = new JacksonFactory(); HttpTransport httpTransport = new NetHttpTransport(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(httpTransport, jsonFactory, key, secret, Arrays.asList("https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email")); try {/*from w w w .j a va 2 s.c o m*/ // google will tell the users browser to go to this address once // they are done authing. StringBuffer callbackURL = request.getRequestURL(); int index = callbackURL.lastIndexOf("/"); callbackURL.replace(index, callbackURL.length(), "").append("/GoogleCallback"); request.getSession().setAttribute("google", flow); String authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(callbackURL.toString()).build(); // send the user to google to be authenticated. response.sendRedirect(authorizationUrl); } catch (Exception e) { throw new ServletException(e); } }
From source file:org.gameontext.auth.google.GoogleAuth.java
License:Apache License
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JsonFactory jsonFactory = new JacksonFactory(); HttpTransport httpTransport = new NetHttpTransport(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(httpTransport, jsonFactory, key, secret, Arrays.asList("https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email")); try {/*from w ww .j a v a 2s. c om*/ // google will tell the users browser to go to this address once // they are done authing. String callbackURL = authURL + "/GoogleCallback"; request.getSession().setAttribute("google", flow); String authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(callbackURL).build(); Log.log(Level.FINEST, this, "Google Auth with callback: {0}", authorizationUrl); // send the user to google to be authenticated. response.sendRedirect(authorizationUrl); } catch (Exception e) { throw new ServletException(e); } }