Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining Instagram Access Token

Tags:

instagram

We have a client who has a simple Instagram feature on the site to pull photos by a certain tag. They just noticed it isn't working. Getting an error - invalid access token. I guess since the 1st because of the updates. We didn't used to need an access token since we're not doing anything with users - just tags.

Now it looks like we need one and the documentation makes zero sense on how to obtain one. And it seems like they're not accepting most apps. The app is in sandbox mode too. So I'm assuming it's because it got switched to that? Got no notification of this happening.

The first step in documentation to get an access token is "Direct the user to our authorization url." What does that even mean? There's not a link provided or anything. It also says "Company Name, Contact Email and Privacy Policy URL are required to start a submission." Our app doesn't have a privacy policy... it's just a simple tag feed. I don't understand why everything is so complex to have a simple tag feed.

Is there a wait time to get the app approved..if it gets approved... Do I have to have it approved before getting an access token? This isn't outlined anywhere.

like image 861
pinksharpii Avatar asked Jun 20 '16 22:06

pinksharpii


People also ask

How can I get my Instagram token 2022?

Go to Dashboard and select Products->Instagram Basic Display->Basic Display. Next, scroll down, and you will see the field Generate Token. Click on the Generate Token field to retrieve Instagram access token. Once you click on the Generate Token field, log in with your Instagram credentials.

What's an Instagram access token?

Access Token is an opaque string that identifies a user, app, or page. It can be used by the app to make graph API calls and is unique to each user. Instagram Access Token is essential for the usage of most Instagram based apps.

Does Instagram have a token?

The Authorization Window allows your app to get Authorization Codes and permissions from app users. Authorization Codes can be exchanged for Instagram User Access Tokens, which must be included when querying an app user's profile or their media.


1 Answers

You got it right. As of June 2016 any Instagram API calls require an access token.

Getting an access token is described in the documentation. App approval is not required.

There are two ways to get one: server-side or client-side. The second option (called implicit authentication) can only be used when implicit OAuth is enabled in the client settings (Manage Clients > Edit Client > Security > Disable implicit OAuth). It is disabled by default.

In either case you need to redirect the user to the authorization URL to obtain an access token.

The URL for explicit mode (server side) is:

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

The URL for implicit mode (client side) is:

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

After this you will be redirected to the REDIRECT-URI, which will be passed an argument. For explicit mode this will be a query string with a code, while for implicit mode you will get the access token directly as a hash:

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

For implicit mode you can then get the access token from the window.location.hash in Javascript.

For explicit mode, however, you need to further process the code to obtain the access token. You can read how this can be done in the API Documentation. I'm not going to take this any further here.

The problem is that every user who wants to see your feed needs to login to Instagram (and have an account) in order to view it. In your case this might not be desired. However, there are a few options to get around this (rather annoying) problem:

  1. You can reuse your own (already obtained) access token(s) to display the Instagram feed for every user. You will need to be aware of rate limits for each token. For sandboxed apps this is 500 API calls / hour, while live mode allows 5000 API calls / hour. [source] You could store tokens in a table and use them in a round-robin manner, to allow more API calls. This involves manually obtaining a bunch of tokens which your application can use (the more the better). This might not be the ideal solution considering Instagram doesn't warrant access tokens to have an unlimited lifetime.

  2. You can retreive JSON data without authentication by appending /media/ to a user page URL, as described in this post. No tokens or client IDs are required for this to work. However, this only works for users, not for tags. Besides, Instagram doesn't document this feature so it is not garanteed to work in the future.

  3. You can use an aggregator like Juicer or Dialogfeed instead which will handle access tokens for you. This is usually not free of charge.

I'm also in the process of making an Instagram feed for my website, and this is what I concluded from my research. Please bare with any errors I made.


Edit: Here are some more limitations for sandbox apps.

In sandbox mode you can only access data from sandbox users (thus users who received a sandbox invite). This means that:

  • Media retreived by user, e.g. /users/{user-id}/media/recent, will return an empty response if the user is not any of the sandbox users.
  • Media retreived by tag, e.g. /tags/{tag-name}/media/recent, will only contain tagged media belonging to sandbox users.

Thus, for a tag feed to work, it needs to be live (reviewed and approved). If you don't want to do this, the only alternative is to use an aggregator as I mentioned above.

like image 52
Midas Avatar answered Nov 11 '22 01:11

Midas