@Override public GoogleAuthorizationCodeTokenRequest setScopes(Collection<String> scopes) { return (GoogleAuthorizationCodeTokenRequest) super.setScopes(scopes); }
/** * Handles the callback from the OAuth provider, returning a valid Credential if successful. * @param code * @param userId * @return * @throws IOException */ public Credential authorize(String code, @Nullable String userId) throws IOException { String redirectUri = getRedirectUrl(); AuthorizationCodeFlow codeFlow = createCodeFlow(); TokenResponse response = codeFlow .newTokenRequest(code) .setRedirectUri(redirectUri) .setScopes(scopes) .execute(); return codeFlow.createAndStoreCredential(response, userId); }
public Credential authorize() { String authorizationUrl = new AuthorizationCodeRequestUrl(AUTHORIZATION_SERVER_URL, OAuth2ClientCredentials.CLIENT_ID).setRedirectUri(URN_IETF_WG_OAUTH_2_0_OOB).setScopes(SCOPES).build(); System.out.println("Please point your browser to the following URL and copy the access token provided after having authorized this application to access your Google Drive:"); System.out.println(authorizationUrl); String code = readCode(); AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl( TOKEN_SERVER_URL), new ClientParametersAuthentication(OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET), OAuth2ClientCredentials.CLIENT_ID, AUTHORIZATION_SERVER_URL ).setScopes(SCOPES).build(); try { TokenResponse response = codeFlow.newTokenRequest(code).setScopes(SCOPES).setRedirectUri(URN_IETF_WG_OAUTH_2_0_OOB).execute(); return codeFlow.createAndStoreCredential(response, null); } catch (IOException e) { throw new JDriveSyncException(JDriveSyncException.Reason.IOException, "Failed to execute token request and store credentials: " + e.getMessage(), e); } }
/** * Authenticates using the authorization code for the user and stores the Credential in the * underlying {@link DataStore}. * @throws AuthException If the credential could not be created. */ public Credential authenticate(String authorizationCode, String userId) throws AuthException { Preconditions.checkNotNull(authorizationCode, "Authorization code must not be null"); AuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(authorizationCode); TokenResponse tokenResponse; try { tokenResponse = tokenRequest .setRedirectUri(redirectUri) .setScopes(scopes) .execute(); } catch (IOException e) { throw new AuthException("Unable to request token.", e); } try { return authorizationCodeFlow.createAndStoreCredential(tokenResponse, userId); } catch (IOException e) { throw new AuthException("Unable to create and store credential.", e); } }
@Override public AuthorizationCodeTokenRequest newTokenRequest(String authorizationCode) { OneDriveAuthorizationCodeTokenRequest tokenRequest = new OneDriveAuthorizationCodeTokenRequest(getTransport(), getJsonFactory(), new GenericUrl(getTokenServerEncodedUrl()), authorizationCode); tokenRequest.set(RESOURCE_PARAMETER, businessResource); return tokenRequest.setClientAuthentication(getClientAuthentication()) .setRequestInitializer(getRequestInitializer()) .setScopes(getScopes()); }
@Override public Credential handleAuthorizationCallback(HttpServletRequest request) { // Checking if there was an error such as the user denied access String error = getError(request); if (error != null) { throw new NuxeoException("There was an error: \"" + error + "\"."); } // Checking conditions on the "code" URL parameter String code = getAuthorizationCode(request); if (code == null) { throw new NuxeoException("There is not code provided as QueryParam."); } try { AuthorizationCodeFlow flow = getAuthorizationCodeFlow(); String redirectUri = getCallbackUrl(request); TokenResponse tokenResponse = flow.newTokenRequest(code) .setScopes(scopes.isEmpty() ? null : scopes) // some providers do not // support the 'scopes' param .setRedirectUri(redirectUri) .execute(); // Create a unique userId to use with the credential store String userId = getOrCreateServiceUser(request, tokenResponse.getAccessToken()); return flow.createAndStoreCredential(tokenResponse, userId); } catch (IOException e) { throw new NuxeoException("Failed to retrieve credential", e); } }
public Credential authorize(VerificationCodeReceiver receiver, String redirectUri) throws IOException { String code = receiver.waitForCode(); AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(TOKEN_SERVER_URL), new ClientParametersAuthentication( OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET), OAuth2ClientCredentials.CLIENT_ID, AUTHORIZATION_SERVER_URL).setScopes(SCOPE).build(); TokenResponse response = codeFlow.newTokenRequest(code) .setRedirectUri(redirectUri).setScopes(SCOPE).execute(); return codeFlow.createAndStoreCredential(response, null); }
.setRedirectUri(redirectUri).setScopes(scopes.isEmpty() ? null : scopes) .executeUnparsed().parseAs(PermissiveTokenResponse.class).toTokenResponse();
public Credential authorize(VerificationCodeReceiver receiver, String redirectUri) throws IOException { String code = receiver.waitForCode(); AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), HTTP_TRANSPORT, JSON_FACTORY, new GenericUrl(TOKEN_SERVER_URL), new ClientParametersAuthentication( BaseCloudExample.getAPIKey(), BaseCloudExample.getAPISecret()), BaseCloudExample.getAPIKey(), AUTHORIZATION_SERVER_URL).setScopes(SCOPES).build(); TokenResponse response = codeFlow.newTokenRequest(code) .setRedirectUri(redirectUri).setScopes(SCOPES).execute(); return codeFlow.createAndStoreCredential(response, null); }
}) .setRedirectUri(findRedirectUrl(requestUrl)) .setScopes(scopes) .execute(); String userId = getUserFromUrl(authorizationCodeResponseUrl);