@Test public void authenticate_whenThereAreNoScopes() throws Exception { String authorizationCode = "authorizationCode"; String expectedRequestContent = "code=authorizationCode&grant_type=authorization_code" + "&redirect_uri=http%3A%2F%2Fredirect" + "&client_id=CLIENT_ID&client_secret=CLIENT_SECRET"; OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setRedirectUri("http: .setHttpTransport(mockHttpTransport) .build(); Credential credential = oAuth2Credentials.authenticate(authorizationCode, "userId"); assertEquals("Request URL did not match.", TOKEN_REQUEST_URL, mockHttpTransport.lastRequestUrl); assertEquals("Request content did not match.", expectedRequestContent, mockHttpTransport.lastRequestContent); assertEquals("Refresh token does not match.", "refreshToken", credential.getRefreshToken()); assertTrue("Expected expires_in between 0 and 3600. Was actually: " + credential.getExpiresInSeconds(), credential.getExpiresInSeconds() > 0 && credential.getExpiresInSeconds() <= 3600); assertEquals("Access token does not match.", "accessToken", credential.getAccessToken()); assertEquals("Access method (Bearer) does not match", BearerToken.authorizationHeaderAccessMethod().getClass(), credential.getMethod().getClass()); }
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); } }
OAuth2Credentials { 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); } } }
OAuth2Credentials { 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); } } private  OAuth2Credentials(); }
OAuth2Credentials { 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); } } private  OAuth2Credentials(); @Nullable String getAuthorizationUrl(); AuthorizationCodeFlow getAuthorizationCodeFlow(); Credential authenticate(String authorizationCode, String userId); Credential loadCredential(String userId); void clearCredential(String userId); String getRedirectUri(); }
OAuth2Credentials { 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); } } private  OAuth2Credentials(); @Nullable String getAuthorizationUrl(); AuthorizationCodeFlow getAuthorizationCodeFlow(); Credential authenticate(String authorizationCode, String userId); Credential loadCredential(String userId); void clearCredential(String userId); String getRedirectUri(); static final String AUTHORIZATION_PATH; static final String TOKEN_PATH; }