Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating instagram authentication in android

I am building an android application that uses the instagram APIs and i would like to know if there's a solution that avoids building an api (redirect uri) to handle redirect from instragram authentication endpoint for token retrieval for an android client.

like image 259
zeddysoft Avatar asked Dec 07 '16 00:12

zeddysoft


1 Answers

I think you can make it with their Client side (implicit) authentication. Check the section at the end of the article.

Once the user has authenticated and then authorized your application, Instagram redirects them to your redirect_uri with the access_token in the url fragment. It will look like this:

http://your-redirect-uri#access_token=ACCESS-TOKEN

Simply grab the access_token off the URL fragment and you’re good to go. If the user chooses not to authorize your application, you’ll receive the same error response as in the explicit flow.

The key part is that you have to define a redirect uri, but in this case it has not to be valid. For example, if you use www.google.com as a redirect uri, after the authentication user will be redirected to

http://www.google.com#access_token=ACCESS-TOKEN

At this point, you must not let the user go forward. Instead, catch the redirect and parse the URI string so you can have your access token.

If your navigation happens inside a WebView, this will likely happen in the shouldOverrideUrlLoading() callback (from WebViewClient):

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("http://www.google.com") { // redirect uri!
        String accessToken = ... // get token from url
        return true; // don’t load the page
    }
    return false;
}

It will be somewhat painful, for example for error handling, but hey, if they have a client side authentication, it should work for you too.

like image 188
natario Avatar answered Sep 24 '22 18:09

natario