22

I have an app, that pulls data from my Instagram account. I authorized this app once, and got access token. But I'm worried, what if this token expires? Should I authorized the app each time the token expires? Can I receive another access token from the auth code? If no, what can I do to have my app always pulling data from account without my participation? Thanks.

mstar0125
  • 261
  • 1
  • 2
  • 5
  • This may happen if you change password of your instagram account you used to authorize app; – itzmukeshy7 Mar 15 '16 at 10:14
  • 4
    Per the [updated IG docs](https://www.instagram.com/developer/authentication/), auth tokens can and will expire at arbitrary times (I'm currently starting to see this behavior, which is fairly catastrophic). There are no refresh tokens and there is no ability to renew expired tokens; you must go through the user oauth process to get a new token. – kungphu Jun 15 '16 at 01:27

4 Answers4

30

But I'm worried, what if this token expires?

AFAIK, Instagram accesstokens don't expire currently.

Note: From Instagram documents.

Note that we do not include an expiry time. Our access_tokens have no explicit expiry, though your app should handle the case that either the user revokes access or we expire the token after some period of time. In this case, your response’s meta will contain an “error_type=OAuthAccessTokenError”. In other words: do do not assume your access_token is valid forever.

Should I authorize the app each time the token expires?

At the moment, you do not need to do that, as token does not expire. As and when the token expires in future, a corresponding warning or error code and message will be sent to you, which you need to handle.

what can I do to have my app always pulling data from account without my participation?

You can try following:

  1. Use sharedpreference to store the accesstoken.

  2. First time when you try to fetch data that needs accesstoken(Authenticated requests), first check in the sharedpreference whether the accesstoken is stored or not. if yes then you don't need to login, just use that accesstoken. If you don't have the accesstoken in preference then do the login using instagram credentials, get the accesstoken and then share it in shared preference and use that for subsequent requests.

  3. You can provide instagram logout option in which you just need to clear the accesstoken from sharedprefernce.

Hope this is helpful to you.

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
7

Update - This no longer works as mentioned in the comments


While not well documented, It's worth pointing out that retrieving a feed of posts does not require an accessToken.

https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token=ACCESS-TOKEN

can also be called passing just your client_id

https://api.instagram.com/v1/users/{user-id}/media/recent/?client_id=CLIENT_ID

This isn't made clear in their documentation though.

https://instagram.com/developer/endpoints/users/

Tom
  • 12,591
  • 13
  • 72
  • 112
  • It actually is documented here. Very top. https://instagram.com/developer/endpoints/ – Sean May 28 '15 at 14:02
  • 3
    I had missed that, if looking directly at the specific endpoint documentation, ie https://instagram.com/developer/endpoints/media/ it's easily missed – Tom Sep 12 '15 at 19:20
  • I agree it's not clear enough that some endpoints can be accessed without an access token. – Hayko Koryun Sep 20 '15 at 10:38
  • 25
    This has changed with Instagram's new API policy (17/11/2015). You can still access the API with client_id if your application was registered prior to 17/11, but it will stop working June 2016. – twined Nov 20 '15 at 12:23
  • 3
    Yep. Stopped working week ago and caused a lot of pain to plenty of us. – Maciej Paprocki Jun 16 '16 at 09:16
2

The Instagram access token expires at arbitrary times. Twice in the in the last 7 months from my experience.

I did not find any way to regenerate the token without my participation, the only valid solution IMHO is to avoid the official API and use something like: https://stackoverflow.com/a/33783840/

fain182
  • 1,068
  • 11
  • 25
2

For the last 3 week, I have created new token multiple time and its expire after 2 days, but I also notice that some token didn't expired which were created before 3 years. Also, Instagram docs say the token can expire any time in the near future.

There is a hack at this moment to show the user's last post just append URL with `/?__a=1'' after user name such as

https://www.instagram.com/vaseem.ishak/?__a=1

you will get user last post with image text, no of comment like etc.just dig little the return JSON

Dexture
  • 976
  • 1
  • 11
  • 29