resp.sendRedirect(oAuth2Credentials.getAuthorizationUrl());
/** * Authenticate the given user. If you are distributing an installed application, this method * should exist on your server so that the client ID and secret are not shared with the end * user. */ private static Credential authenticate(String userId, SessionConfiguration config) throws Exception { OAuth2Credentials oAuth2Credentials = createOAuth2Credentials(config); // First try to load an existing Credential. If that credential is null, authenticate the user. Credential credential = oAuth2Credentials.loadCredential(userId); if (credential == null || credential.getAccessToken() == null) { // Send user to authorize your application. System.out.printf("Add the following redirect URI to your developer.uber.com application: %s%n", oAuth2Credentials.getRedirectUri()); System.out.println("Press Enter when done."); System.in.read(); // Generate an authorization URL. String authorizationUrl = oAuth2Credentials.getAuthorizationUrl(); System.out.printf("In your browser, navigate to: %s%n", authorizationUrl); System.out.println("Waiting for authentication..."); // Wait for the authorization code. String authorizationCode = localServerReceiver.waitForCode(); System.out.println("Authentication received."); // Authenticate the user with the authorization code. credential = oAuth2Credentials.authenticate(authorizationCode, userId); } localServerReceiver.stop(); return credential; }
@Test public void getAuthorizationUrl_whenThereAreCustomScopes() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setScopes(Arrays.asList(Scope.PROFILE)) .setCustomScopes(Arrays.asList("custom")) .build(); assertTrue( "https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID&response_type=code&scope=custom%20profile" .equals(oAuth2Credentials.getAuthorizationUrl()) || "https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID&response_type=code&scope=profile%20custom" .equals(oAuth2Credentials.getAuthorizationUrl())); }
@Test public void getAuthorizationUrl_whenThereAreDuplicateCustomScopes() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setScopes(Arrays.asList(Scope.PROFILE)) .setCustomScopes(Arrays.asList("profile")) .build(); assertEquals("https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID&response_type=code&scope=profile", oAuth2Credentials.getAuthorizationUrl()); }
@Test public void getAuthorizationUrl_whenThereIsARedirectUri() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setRedirectUri("https://localhost:8181/OAuth2Callback") .setScopes(Arrays.asList(Scope.PROFILE)) .setCustomScopes(Arrays.asList("profile")) .build(); assertEquals("https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID" + "&response_type=code&scope=profile&redirect_uri=https%3A%2F%2Flocalhost%3A8181%2FOAuth2Callback", oAuth2Credentials.getAuthorizationUrl()); }
@Test public void getAuthorizationUrl() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setScopes(Arrays.asList(Scope.PROFILE, Scope.REQUEST, Scope.HISTORY)) .build(); assertEquals("https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID" + "&response_type=code&scope=history%20profile%20request", oAuth2Credentials.getAuthorizationUrl()); }
@Test public void getAuthorizationUrl_whenThereIsAnEmptyScopeList() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .setScopes(new ArrayList<Scope>()) .build(); assertEquals("https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID&response_type=code", oAuth2Credentials.getAuthorizationUrl()); }
@Test public void getAuthorizationUrl_whenThereAreNoScopes() throws Exception { OAuth2Credentials oAuth2Credentials = new OAuth2Credentials.Builder() .setClientSecrets("CLIENT_ID", "CLIENT_SECRET") .build(); assertEquals("https://login.uber.com/oauth/v2/authorize?client_id=CLIENT_ID&response_type=code", oAuth2Credentials.getAuthorizationUrl()); }