How to make sure that my iOS application has a valid access token for using the Instagram API

advertisements

I'm building an iOS app that will use instagram photos in a slide show as the background of the app.

What I want to do is just set up a specific account that I can upload pictures to, and then the app will pull in the most recent photos from this account.

So far, I've set up the account and have been able to generate an access token manually by inserting my client id and redirect URI into this URL

https://instagram.com/oauth/authorize/?client_id=[CLIENT_ID]&redirect_uri=[REDIRECT_URI]&response_type=code

However, I've read that the access token generated from following this procedure is not permanent. I do not want the users of my app to ever see the authentication going on in the background. They themselves will never actually login into Instagram.

What would be the best way of making sure my app is always authenticated at launch and that the access token is always valid?

Thanks


A typical OAuth flow has the resource owner (a user) approve or deny requests from a client application. When you first got an access token, you had to complete a form approving access to Intsagram by your app.

Since you want to hide the auth_server/resource_owner interaction from your end users, you'll have to automate the role of the resource owner. The access token should tell you when it expires. Since it's your redirection endpoint that has the access token, that's where you'll need code to detect the token will soon expire and request a new one. Your code will need to

  1. Simulate a request from the client app by going to https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

  2. Respond to the HTML page that is returned. Approve the request.

  3. The server will respond with an authorization code that you can exchange for a new access token.

There are some hoops to jump through because OAuth is designed for the resource owner to approve or deny each request.