tokenResponse = new GoogleAuthorizationCodeTokenRequest(TRANSPORT, JSON_FACTORY, clientID, clientSecret, code, returnURL).execute(); } catch (IOException ioe) { throw new SocialException(SocialExceptionCode.INVALID_CLIENT, "Error when obtaining access token from Google: " + ioe.getMessage(), ioe);
/** * @param transport HTTP transport * @param jsonFactory JSON factory * @param tokenServerEncodedUrl token server encoded URL * @param clientId client identifier issued to the client during the registration process * @param clientSecret client secret * @param code authorization code generated by the authorization server * @param redirectUri redirect URL parameter matching the redirect URL parameter in the * authorization request (see {@link #setRedirectUri(String)} * * @since 1.12 */ public GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String tokenServerEncodedUrl, String clientId, String clientSecret, String code, String redirectUri) { super(transport, jsonFactory, new GenericUrl(tokenServerEncodedUrl), code); setClientAuthentication(new ClientParametersAuthentication(clientId, clientSecret)); setRedirectUri(redirectUri); }
@Override public GoogleTokenResponse execute() throws IOException { return executeUnparsed().parseAs(GoogleTokenResponse.class); }
@Override public GoogleAuthorizationCodeTokenRequest newTokenRequest(String authorizationCode) { // don't need to specify clientId & clientSecret because specifying clientAuthentication // don't want to specify redirectUri to give control of it to user of this class return new GoogleAuthorizationCodeTokenRequest(getTransport(), getJsonFactory(), getTokenServerEncodedUrl(), "", "", authorizationCode, "").setClientAuthentication( getClientAuthentication()) .setRequestInitializer(getRequestInitializer()).setScopes(getScopes()); }
GoogleAuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow .Builder(httpTransport, jsonFactory, clientId, clientSecret, scopes) .setCredentialDataStore(new MemoryDataStoreFactory().getDataStore("tokens")) .build(); String userId = "user-id"; Credential credential = authorizationCodeFlow.loadCredential(userId); if (credential == null) { GoogleAuthorizationCodeRequestUrl authorizationUrl = authorizationCodeFlow.newAuthorizationUrl(); authorizationUrl.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI); LOGGER.error("Please, authorize application. Visit {}", authorizationUrl); Scanner s = new Scanner(System.in); String code = s.nextLine(); GoogleAuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(code); tokenRequest.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI); GoogleTokenResponse tokenResponse = tokenRequest.execute(); credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, userId); }
GoogleAuthorizationCodeTokenRequest authorizationTokenRequest = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, request.getParameter("code"), REDIRECT_URL); GoogleTokenResponse tokenResponse = authorizationTokenRequest.execute();
TokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUrl).execute(); Credential credential = flow.createAndStoreCredential(response, "userID"); com.google.api.services.calendar.Calendar client =
private Credential getNewOAuth2Credential() throws OAuthException { GoogleAuthorizationCodeFlow authorizationFlow = getAuthorizationFlow(); String authorizeUrl = authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build(); System.out.println("\n**ACTION REQUIRED** Paste this url in your browser" + " and authenticate using your **AdWords Admin Account**: \n\n" + authorizeUrl + '\n'); // Wait for the authorization code. System.out.println("Type the code you received on the web page here: "); try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) { String authorizationCode = reader.readLine(); // Authorize the OAuth2 token. GoogleAuthorizationCodeTokenRequest tokenRequest = authorizationFlow.newTokenRequest(authorizationCode); tokenRequest.setRedirectUri(CALLBACK_URL); GoogleTokenResponse tokenResponse = tokenRequest.execute(); // Create the credential. Credential credential = new GoogleCredential.Builder() .setClientSecrets(clientId, clientSecret) .setJsonFactory(new JacksonFactory()) .setTransport(new NetHttpTransport()) .build() .setFromTokenResponse(tokenResponse); return credential; } catch (IOException e) { throw new OAuthException("An error occured obtaining the OAuth2Credential", e); } }
GoogleTokenResponse googleTokenResponse = new GoogleAuthorizationCodeTokenRequest( httpTransport, jsonFactory, "https://www.googleapis.com/oauth2/v3/token", clientId, clientSecret, authCode.getAuthCode(), redirectURIs).execute(); resJson.append(DBConstants.GSpread.ACCESS_TOKEN, googleTokenResponse.getAccessToken()); resJson.append(DBConstants.GSpread.REFRESH_TOKEN, googleTokenResponse.getRefreshToken());
.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI) .execute();
/** * Retrieve OAuth 2.0 credentials. * * @return OAuth 2.0 Credential instance. * @throws IOException */ private Credential getCredentials() throws IOException { String code = tokenField.getText(); HttpTransport transport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); String CLIENT_SECRET = "EPME5fbwiNLCcMsnj3jVoXeY"; // Step 2: Exchange --> GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest( transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, code, REDIRECT_URI).execute(); // End of Step 2 <-- // Build a new GoogleCredential instance and return it. return new GoogleCredential.Builder() .setClientSecrets(CLIENT_ID, CLIENT_SECRET) .setJsonFactory(jsonFactory).setTransport(transport).build() .setAccessToken(response.getAccessToken()) .setRefreshToken(response.getRefreshToken()); }