1

In order to make request to the instagram api. I require an access token.

Which is documented here. http://instagram.com/developer/authentication/

These access token can change so i need to lookup via the api to get the "code" and then the access token. Most of the examples i see online prompt the user to instagram which then calls the callback url and off you go and generate the access_token.

How can i via the api ( and not logging into instragram ) get the inital "code"/

I was hoping for something like this.

 var accessUrl = string.Format(
                    "https://instagram.com/oauth/authorize/?client_id={0}&redirect_uri={1}&response_type=code",
                    instagramClientId, redirectUrl);

            WebRequest request = WebRequest.Create(accessUrl);
            WebResponse response = request.GetResponse();

To be clear I'm not asking how to get the access_token but how do I get the "code" than generates the access_token without logging into instagram

Cœur
  • 37,241
  • 25
  • 195
  • 267
frosty
  • 5,330
  • 18
  • 85
  • 122
  • Further info. I was wanting to display recent post from a userid. The docs indicated that i need an auth_token to do this. But i was able to get the following working. http://stackoverflow.com/questions/17373886/how-can-i-get-a-users-media-from-instagram-without-authenticating-as-a-user# – frosty Dec 12 '13 at 15:35

1 Answers1

1

code is appended as a url querystring in a GET request that Instagram sends to your callback url.

For example, if your callback url is:

http://www.mycallbackurl.com/callback

Instagram (upon receiving your access_token request) will send a GET call to:

http://mycallbackurl.com/callback?code=20938409823kjl2k3j4o998as0df809s80980234809

(it's base64 encoded).

You then need to capture that url parameter on your callback server and echo it out on the page (nothing else, only the value of the code parameter), resulting in something like:

<html>
    <body>20938409823kjl2k3j4o998as0df809s80980234809</body>
</html>

This can be done easily with php:

if (isset($_GET['code']))
    echo $_GET['code'];
else
    //do all the other stuff your page does

Hopefully if you're not using PHP you can discern how to accomplish the same result. This part of their authentication flow is rather poorly documented, but hopefully this helps.

Also: (Not sure if I understand this part of the question or not, but) the only authentication flow they allow is the in-browser login page. If you're trying to build your own custom login and pass credentials, it won't work.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • Hi, thanks for the detailed response. To confirm are you saying there is no way to authenticate and in turn get the access code without logging into the instagram website. It's seem worrying that i could add this code and at any stage the access code could expire without a way to renew. – frosty Dec 11 '13 at 23:30
  • 1
    You're going to need to have this available publicly all the time for anyone who authenticates. If the token expires, you need to pop up the login auth view again. It's an awful method of authentication, and I truly wish they offered a full OAuth2 authentication handshake that could be done just with `GET`. But they don't, which is why all the 3rd party apps you see out there have the Authorize page appear after you log in. – brandonscript Dec 11 '13 at 23:34