com.google.api.services.oauth2.Oauth2 Java Examples
The following examples show how to use
com.google.api.services.oauth2.Oauth2.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleIdTokenAuth.java From styx with Apache License 2.0 | 6 votes |
private String getServiceAccountIdTokenUsingAccessToken(GoogleCredentials credentials, String targetAudience) throws IOException { final Oauth2 oauth2 = new Oauth2.Builder(httpTransport, JSON_FACTORY, null) .build(); final AccessToken accessToken = accessToken(withScopes(credentials, ImmutableList.of("https://www.googleapis.com/auth/userinfo.email"))); final Tokeninfo info = oauth2.tokeninfo() .setAccessToken(accessToken.getTokenValue()) .execute(); final String principal = info.getEmail(); if (principal == null) { throw new IOException("Unable to look up principal email, credentials missing email scope?"); } if (!SERVICE_ACCOUNT_PATTERN.matcher(principal).matches()) { throw new IOException("Principal is not a service account, unable to acquire id token: " + principal); } return getServiceAccountIdTokenUsingAccessToken(credentials, principal, targetAudience); }
Example #2
Source File: BaseServlet.java From drivemarks with Apache License 2.0 | 6 votes |
/** * If OAuth2 redirect callback is invoked and there is a code query param, * retrieve user credentials and profile. Then, redirect to the home page. * @param req Request object. * @param resp Response object. * @throws IOException */ protected boolean handleCallbackIfRequired(HttpServletRequest req, HttpServletResponse resp) throws IOException { String code = req.getParameter("code"); if (code != null) { // retrieve new credentials with code Credential credential = credentialManager.retrieve(code); // request userinfo Oauth2 service = getOauth2Service(credential); Userinfo about = service.userinfo().get().execute(); String id = about.getId(); credentialManager.save(id, credential); req.getSession().setAttribute(KEY_SESSION_USERID, id); String redirect = (String) req.getSession().getAttribute(KEY_SESSION_REDIRECT); req.getSession().setAttribute(KEY_SESSION_REDIRECT, null); if (redirect != null) { resp.sendRedirect(redirect); } else { resp.sendRedirect("?"); } return true; } return false; }
Example #3
Source File: Utils.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Obtain end-user authorization grant for Google APIs and return username */ public static String getUserInfo(Credential credential) throws IOException { Oauth2 oauth2Client = new Oauth2.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(APP_NAME) .build(); // Retrieve user profile Userinfoplus userInfo = oauth2Client.userinfo().get().execute(); String username = userInfo.getGivenName(); return username; }
Example #4
Source File: BaseServlet.java From drivemarks with Apache License 2.0 | 2 votes |
/** * Build and return an Oauth2 service object based on given request parameters. * @param credential User credentials. * @return Drive service object that is ready to make requests, or null if * there was a problem. */ protected Oauth2 getOauth2Service(Credential credential) { return new Oauth2.Builder(TRANSPORT, JSON_FACTORY, credential).build(); }