Search code examples
instagram-api

How to handle authentication with instagram, if the user is already logged in with instagram in some different window


I am integrating with Instagram in my angular application.

To integrate with instagram, I am following below 3 steps according to instagram developers api https://www.instagram.com/developer/authentication/

Step One: Direct your user to our authorization URL

https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

Step Two: Receive the redirect from Instagram

http://your-redirect-uri?code=CODE

Step Three: Request the access_token

Then i am exchanging the code received in the previous step for an access token.

 curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token

The above steps work fine if the user is not logged in some other window. But if the user is already logged in with instagram in some other window,

then the step 3 works sometimes or sometimes it gives me following error

{"error_type"=>"OAuthException", "error_message"=>"Matching code was not found or was already used.", "code"=>400}

Is there something am i missing??


Solution

  • before allowing user to login, do a instagram logout by opening the instagram logout url in a hidden iframe, something like this:

    $(document).append('<div style="display:none"><iframe src="https://instagram.com/accounts/logout/" width="0" height="0"></iframe></div>');
    

    this will logout of instagram