wuman / android-oauth-client

Android OAuth Client (android-oauth-client) is a library that helps to easily add an OAuth flow to an existing Android application.
Apache License 2.0
425 stars 107 forks source link

DialogFragmentController's getRedirectUri() cannot return custom URL scheme #63

Open garylai opened 8 years ago

garylai commented 8 years ago

We are implementing OAuth2 flow on android. Our own OAuths system uses custom URL to redirect from authorising page back to the app so that the app can get the authorization_token for exchanging access_token with the OAuth server. Therefore we return our custom url in getRedirectUri() of our AuthorizationUIController.

But It now throws: java.lang.IllegalArgumentException: java.net.MalformedURLException: Unknown protocol: {our custom scheme}

I would like to ask how thie library exchange authorization_token for access_token and if it supports custom URL redirection?

pradeepgudipati commented 7 years ago

Actually, this library uses Web view, so there is no custom uri scheme required. The default http://localhost/Callback is more than enough.

simonzettervall commented 7 years ago

I have the same problem, I need to use a custom scheme as well because the backend will only accept myapp://oauth-callback.

simonzettervall commented 7 years ago

I fixed this by this code, which of course is not the best practice but it did get the job done (not sure if any problems will arise because of this, but it surely will):

        /**
     * Sets up a url stream handler factory. This is necessary as we have a custom scheme/protocol and this in conjunction with our authentication library requires a handler to be declared, even
     * though this will do nothing.
     */
    private void setupURLStreamHandlerFactory() {
        URLStreamHandlerFactory urlStreamHandlerFactory = new URLStreamHandlerFactory() {
            @Override
            public URLStreamHandler createURLStreamHandler(String protocol) {
                URLStreamHandler urlStreamHandler;

                if (protocol.equals(mScheme)) {
                    urlStreamHandler = new URLStreamHandler() {
                        @Override
                        protected URLConnection openConnection(URL u) throws IOException {
                            return null;
                        }
                    };
                }

                else {
                    urlStreamHandler = null;
                }

                return urlStreamHandler;
            }
        };

        URL.setURLStreamHandlerFactory(urlStreamHandlerFactory);
    }