dmfs/oauth2-essentials

NPE error when using sample code

HolimaX opened this issue · 3 comments

There seems to be issue with sample, provided in Readme. The code cannot resolve the 'redirectUri' variable. I tried to workaround by using 'client.', but it fails. See below for details.

When using:

                        HttpRequestExecutor executor = new HttpUrlConnectionExecutor();

                        // Create OAuth2 provider
                        OAuth2AuthorizationProvider provider = new BasicOAuth2AuthorizationProvider(
                                URI.create("https://.../oauth/authorize"),
                                URI.create("https://.../oauth/token"),
                                new Duration(1, 0, 3600) /* default expiration time in case the server doesn't return any */);

                        // Create OAuth2 client credentials
                        OAuth2ClientCredentials credentials = new BasicOAuth2ClientCredentials(
                                list.get(2).toString(), "...");

                        // Create OAuth2 client
                        OAuth2Client client = new BasicOAuth2Client(
                                provider,
                                credentials,
                                new LazyUri(new Precoded(redirectUrl)) /* Redirect URL */);

                        // Start an interactive Authorization Code Grant
                        OAuth2InteractiveGrant grant = new AuthorizationCodeGrant(
                                client, new BasicScope("blablabla"));

                        // Get the authorization URL and open it in a WebView
                        URI authorizationUrl = grant.authorizationUrl();

                        // Open the URL in a WebView and wait for the redirect to the redirect URL
                        // After the redirect, feed the URL to the grant to retrieve the access token
                        OAuth2AccessToken token = grant.withRedirect(client.redirectUri()).accessToken(executor);

I am receiving:
java.util.NoSuchElementException: No value is present in this Optional. Better call isPresent() next time.

In methoid call:
OAuth2AccessToken token = grant.withRedirect(client.redirectUri()).accessToken(executor);

dmfs commented

The redirect URI you pass to withRedirect should be the URL that the server redirected to after the user has granted access. I probably should point that out in the example.

When you finish the interactive Authorization Code Grant in the browser window the OAuth2 auth endpoint redirects to your redirectUri (the one you passed to BasicOAuth2Client) but it appends a URI query parameter which contains the auth code. If that auth code is not present you'll get the said exception in BasicOAuth2AuthCodeAuthorization, line 45.
Your code only passes the base redirect URL, without the query parameters, hence the exception.

For example, if your redirect URL is http://localhost, the server would redirect the browser to http://localhost?code=xyz.

Please make sure you catch and pass the redirect URL you get from the server. Also see the diagram in https://tools.ietf.org/html/rfc6749#section-4.1, what we need is the result of step (C).

The redirect URI you pass to withRedirect should be the URL that the server redirected to after the user has granted access. I probably should point that out in the example.

When you finish the interactive Authorization Code Grant in the browser window the OAuth2 auth endpoint redirects to your redirectUri (the one you passed to BasicOAuth2Client) but it appends a URI query parameter which contains the auth code. If that auth code is not present you'll get the said exception in BasicOAuth2AuthCodeAuthorization, line 45.
Your code only passes the base redirect URL, without the query parameters, hence the exception.

For example, if your redirect URL is http://localhost, the server would redirect the browser to http://localhost?code=xyz.

Please make sure you catch and pass the redirect URL you get from the server. Also see the diagram in https://tools.ietf.org/html/rfc6749#section-4.1, what we need is the result of step (C).

Ok, yeah, I already spent two days trying to get webview working and I did it. Anyhow - some simple example would help to speed up things for other users! :) You can add link to README that this step is needed as per OAUTH2 design....

P.S. Kudos for this library anyways! :)

@HolimaX - Do you have an example with WebView?