Jacob Malachowski
Jacob Malachowski

Reputation: 981

uri.getQueryParameter() returning null value

I have the following code in a class:

@Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url)
        {
            if (url.startsWith(Constants.OAUTH_REDIRECT))
            {
                Uri uri = Uri.parse(url);

                String state = uri.getQueryParameter("state");

                if (state != null && state.equals(Constants.randString))
                {
                    String error = uri.getQueryParameter("error");

                    if (error != null && error.length() > 0)
                    {
                        if (error.equals("access_denied"))
                        {
                            //user chose not to login
                            Log.d("oAuthView", "Access Denied");
                            finish();
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
                            finish();
                        }
                    }

                    //Go back to MainActivity with authorization code
                    Intent resultIntent = getIntent();
                    resultIntent.putExtra("authCode", uri.getQueryParameter("code"));

                    setResult(RESULT_OK, resultIntent);

                    finish();

                    return true;
                }
            }
            return false;
        }

For some reason I am getting a null value when trying to capture "state", even though I have made sure that it is in the URL. This means the app never enters the if statement that follows. I have also checked and the same thing happens when I try to capture the authCode. Any ideas on why they would return null?

Edit: For some reason when I try an alternative URL scheme, it parses correctly.

The URL I need to parse but doesn't work: http://www.website.com/#access_token=tokenstringhere&token_type=bearer&state=randomStringHere&expires_in=3600&scope=identity+submit

A similar URL that does work: http://www.website.com/?state=cnmdr6&code=tokenStringHere

What would cause the first parse to fail but the second one to properly parse?

Upvotes: 3

Views: 3934

Answers (1)

the korovay
the korovay

Reputation: 645

If someone is looking for solution.

Your url is invalid because you missed the question sign ? before query in your url. Your url should look like this: http://www.website.com/?access_token=tokenstringhere&token_type=bearer&state=randomStringHere&expires_in=3600&scope=identity+submit Please check the documentation.

Upvotes: 3

Related Questions