Last active
May 13, 2019 18:14
-
-
Save balvinder294/ecf07defa363317ff92e0550e61e127d to your computer and use it in GitHub Desktop.
Sample Code Google Login Oauth2 Java Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Required for Http Request and json management for credentials | |
HttpTransport httpTransport = new NetHttpTransport(); | |
JacksonFactory jsonFactory = new JacksonFactory(); | |
// Go to the Google API Console, open your application's | |
// credentials page, and copy the client ID and client secret. | |
// Then paste them into the following code. | |
String clientId = "YOUR_CLIENT_ID"; | |
String clientSecret = "YOUR_CLIENT_SECRET"; | |
// Or your redirect URL for web based applications. | |
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; | |
// Default scope is profile, but you can add email and openid for more permissions | |
String scope = "profile email openid"; | |
// Step 1: Create Authorize Url for auhorizing the user --> | |
String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId, redirectUrl, Arrays.asList(scope)) | |
.build(); | |
// Point or redirect your user to the authorizationUrl where he will get access code | |
//Sample code you will get from url | |
code=4%2FSQEGs6ivqKz2YYpdwzgkLRHyr4nq2-hxHUy336-80TgnmJyyQTzjWnly3x5QwDndN3kFNyCMHTaIFGBWauOOoGA | |
// here code is parameter you need to get from URL | |
// End of Step 1 <-- | |
// Step 2. | |
// Authorization code we get after url authentication need to be decoded | |
String decodedAuthorizationCode = URLDecoder.decode(authorizationCode,"UTF-8"); | |
// Create exchange request for Google access Token | |
GoogleTokenResponse googleTokenResponse = new GoogleAuthorizationCodeTokenRequest( | |
httpTransport,JSON_FACTORY, "https://www.googleapis.com/oauth2/v4/token", | |
this.clientId,this.clientSecret, decodedAuthorizationCode, REDIRECT_URI) | |
.execute(); | |
// Extract token from token response | |
String accessTokenFromGoogle = googleTokenResponse.getAccessToken(); | |
// Extract Id Token from response | |
GoogleIdToken googleIdToken = googleTokenResponse.parseIdToken(); | |
//Step 2 End--> | |
//Step 3. Get User data from either Payload from token response object (openid scope is required) | |
// Get payload from id token containing user data | |
GoogleIdToken.Payload payload = googleIdToken.getPayload(); | |
String userId = payload.getSubject(); // Use this value as a key to identify a user. | |
String email = payload.getEmail(); | |
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified()); | |
String name = (String) payload.get("name"); | |
String pictureUrl = (String) payload.get("picture"); | |
String locale = (String) payload.get("locale"); | |
String familyName = (String) payload.get("family_name"); | |
String givenName = (String) payload.get("given_name"); | |
// Step 3 End | |
// Step 4 Get User Info from OAuth2 by access token from previous step no. 2 | |
// Create new google credentials with access token from googl | |
GoogleCredential googleCredential = new GoogleCredential().setAccessToken(accessTokenFromGoogle); | |
// Create Oauth Object with token credential | |
Oauth2 oauth2 = new Oauth2.Builder( | |
httpTransport,jsonFactory,googleCredential) | |
.setHttpRequestInitializer(googleCredential) | |
.setApplicationName("Dehaze") // Change to name of your app | |
.build(); | |
// Create UserInfo Plus object; | |
String id = useririnfoplus.getId(); | |
String email = userinfoplus.getEmail(); | |
String name = useririnfoplus.getName(); // Balvinder Singh | |
String familyName = userinfoplus.getFamilyName(); // Singh | |
String givenName = userinfoplus.getGivenName(); // Balvinder | |
String locale = userinfoplus.getLocale(); // en | |
Boolean emailVerified = userinfoplus.getVerifiedEmail(); // true in case of verified email | |
String profilePicUrl = userinfoplus.getPicture(); // url of profile pic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment