1
votes

I am integrating Instagram login into my application. As per the provided documentation in bellow link: https://www.instagram.com/developer/authentication/

For getting CODE I hit follow API:

https://api.instagram.com/oauth/authorize?app_id="your app_id"&redirect_uri=https://www.google.com/&scope=user_profile,user_media&response_type=code

After hitting above API in web-view I got bellow new URL with code in it:

http://www.google.com/?code=AQCPw4m0jd85IX7Qi83rd-pxsFKDE8Bjb0kZ2pWZ8sarcPlNyaZXwzE1yHHqfVIgDej_ZPYrTh6_BaIafsOWsTZNJbWFDiXsQPTEZHvFBojQ5F91SBSykqjUkG0U1OS6nFyAcwNq-FsbpmBtIPcp45qNXV5_avgCCpBzG-Hndp6JiRbaF2uSgUiOep5-Uu0Ks8DiwVs_7zAfuMasJJ7PAK1gfRDNVOPC-XBYqfdPU1VlkA

In this example my code is

AQCPw4m0jd85IX7Qi83rd-pxsFKDE8Bjb0kZ2pWZ8sarcPlNyaZXwzE1yHHqfVIgDej_ZPYrTh6_BaIafsOWsTZNJbWFDiXsQPTEZHvFBojQ5F91SBSykqjUkG0U1OS6nFyAcwNq-FsbpmBtIPcp45qNXV5_avgCCpBzG-Hndp6JiRbaF2uSgUiOep5-Uu0Ks8DiwVs_7zAfuMasJJ7PAK1gfRDNVOPC-XBYqfdPU1VlkA

I use it in access_token request below:

https://api.instagram.com/oauth/access_token?
app_id="YOUR APP ID"
&app_secret="YOUR SECRET KEY"
&grant_type=authorization_code
&redirect_uri=https%3A%2F%2Fwww.google.com%2F
&code=AQCPw4m0jd85IX7Qi83rd-pxsFKDE8Bjb0kZ2pWZ8sarcPlNyaZXwzE1yHHqfVIgDej_ZPYrTh6_BaIafsOWsTZNJbWFDiXsQPTEZHvFBojQ5F91SBSykqjUkG0U1OS6nFyAcwNq-FsbpmBtIPcp45qNXV5_avgCCpBzG-Hndp6JiRbaF2uSgUiOep5-Uu0Ks8DiwVs_7zAfuMasJJ7PAK1gfRDNVOPC-XBYqfdPU1VlkA

Response of above api hit:

{
    "access_token": "IGQVJVT2hod1dEN3N0UnBwTWllb3pZAaENnS0VveHZARSkNwLVpwS2Uxc0w2VUVOeEJxaUdkMnlQRkdzVGJudnh0cXREZA3BvWHNELTR1UndsOWR2MXFST0JlUG45TEdFVnhiblhScVNlYlJtLUNwQU5Kc2ltZAy1LUlRMaF9ZA",
    "user_id": 17841406439718884
}

Now in order to get User Profile data, I am hitting bellow api using above access_token as mentioned in [link][1] :

Api hit :

https://api.instagram.com/v1/users/self/?access_token="+accesstoken

But in this api, I am getting error which says:

{
    "meta": {
        "code": 400,
        "error_type": "OAuthAccessTokenException",
        "error_message": "The access_token provided is invalid."
    }
}

Please let me know how to get valid accessToken programmatically , I have clearly mentioned every step I follow if you stil need anything for more calrification I willprovide you that too!!!

I have followed each and every step mentioed in this link :

https://developers.facebook.com/docs/instagram-basic-display-api/guides/getting-access-tokens-and-permissions

1
You are using an access token for the old API, with the new basic display API. That won't work. For the old API, tokens seem to expire randomly, so no way to get a permanent or long-lived token. For the new Basic Display API, the tokens expire after 2 hours. - hermanschutte
Stop using Instagram APIs for self details. Use Facebook Graph API. Instagram APIs are all deprecated. It will stop completely in Early 2020. Refer instagram.com/developer - Amrut Bidri
@hermanschutte Are you able to get the acess_token ? I always get a message saying "code doesn't exist or is already used" - Shaharyar Kirmani
@ShaharyarKirmani, Provide secure redirect URL in the developer console. - user2028
Are you sure that the Instagram app installed in your device is the latest one? - Gk Mohammad Emon

1 Answers

3
votes

It is very poorly documented by Facebook and they made it very difficult. I will go through the steps so that you do not suffer as much I suffered...

{
    "access_token": "IGQVJVT2hod1dEN3N0UnBwTWllb3pZAaENnS0VveHZARSkNwLVpwS2Uxc0w2VUVOeEJxaUdkMnlQRkdzVGJudnh0cXREZA3BvWHNELTR1UndsOWR2MXFST0JlUG45TEdFVnhiblhScVNlYlJtLUNwQU5Kc2ltZAy1LUlRMaF9ZA",
    "user_id": 17841406439718884
}

This is a short-lived token which expires in 1 hour; therefore you should use that to generate a long-lived token. before it expires: To do so used that piece link in postman

 "https://graph.instagram.com/access_token
  ?grant_type=ig_exchange_token
  &client_secret={instagram-app-secret}
  &access_token={short-lived-access-token}"

Then you will get as a response

{
  "access_token":"{long-lived-user-access-token}",
  "token_type": "bearer",
  "expires_in": 5183944  // Number of seconds until token expires
}

This is your long term access token and it shows the expiration date which 60 days. Then you should use the request lin

GET https://graph.instagram.com/{media-id}
  ?fields=id,caption,username,
  &access_token={access-token}// Long Lived token

See their website for more data query: https://developers.facebook.com/docs/instagram-basic-display-api/reference/user#fields

I hope that helps you to solve the problem. I am writing a piece code to refresh the long-lived token before its expiration in 60 days. Once I finish it I will share that as well