Instagram Basic Display API: Obtaining an Access Token

In this post we learn how to obtain and access token from the Instagram Basic Display API with a code passed to us from Instagram once the user has authorized our application. The access token we get is short lived meaning it will only be valid for an hour or so. In the next blog post we will learn how to exchange this short lived access token, for a long lived access token which can be used for up to 60 days.

Required to continue: Facebook app with Instagram product added

Step 1: Generate the Authorization URL

This URL will take the user to Instagram and present them with a pop up. The pop up will ask the user to Authorize our app so our app can read the users Instagram information.

app pop up

Authorization URL Structure

https://api.instagram.com/oauth/authorize?app_id={app-id}&redirect_uri={redirect-uri}&scope=user_profile,user_media&response_type=code

  • {app-id}: Instagram App ID. In the developer app dashboard got to “Instagram > Basic Display”
  • {redirect-uri}: This must match a URI that has been entered into the apps “Valid OAuth Redirect URIs” in the apps dashboard.

 
app creds

Once you have the Authorization URL, place it inside of an <a> tag for the user to click on.

<!-- Link to take user to Instagram for authorization with our app -->
<a href="https://api.instagram.com/oauth/authorize?app_id={app-id}&redirect_uri={redirect-uri}&scope=user_profile,user_media&response_type=code">
    Authorize W/Instagram
</a>

Step 2: Obtaining the Access Token

When the user Authorizes our app, Instagram sends them to the {redirect-uri} we passed along in the authorization URL and appends on a code. On our website, we will check for this code in the URL. If code exists in the URL, use it to get an access token. If no code variable exists in the URL, display the “Authorize W/Instagram” link from step 1 above. The URL will look like this.

Instagram Redirect URI with code: {redirect-uri}?code={ig-code}
 

Access Token API Endpoint

Endpoint
https://api.instagram.com/oauth/access_token

Type
POST

Params

  • app_id: Instagram App ID. In the developer app dashboard go to “Instagram > Basic Display” and copy the “Instagram App ID”.
  • app_secret: Instagram App Secret. In the developer app dashboard go to “Instagram > Basic Display” and copy the “Instagram App Secret”.
  • redirect_uri: This must match one of the URIs that has been entered into the apps “Valid OAuth Redirect URIs” in the apps dashboard.
  • grant_type: authorization_code
  • code: {ig-code}

 

PHP CURL call to the Access Token API Endpoint

// base endpoint
$endpoint = 'https://api.instagram.com/oauth/access_token';

// params the endpoint requires
$params = array(
	'app_id' => 'INSTAGRAM_APP_ID', // our instagram app id
	'app_secret' => 'INSTAGRAM_APP_SECRET', // our instagram app secret
	'grant_type' => 'authorization_code',
	'redirect_uri' => 'INSTAGRAM_APP_REDIRECT_URI', // our redirect uri
	'code' => $_GET['code'] // code instagram sent us in the URL
);			

// open curl call
$ch = curl_init();

// set type to post and pass the fields along 
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
curl_setopt( $ch, CURLOPT_POST, 1 );

// set curl url
curl_setopt( $ch, CURLOPT_URL, $endpoint );

// set other curl options
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); 
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

// get response, close curl, make php array
$response = curl_exec( $ch );

// close cur
curl_close( $ch );

// make nice php array :D
$responseArray = json_decode( $response, true );

// print out the response array that contains the access token
echo '<pre>';
print_r( $responseArray );

This CURL call hits our Instagram endpoint with the required parameters and receives a response from Instagram with an access token. If any of the parameters are incorrect, an error message will be returned. The access token here is a short lived access token meaning it is not valid for very long. In the next post, this short lived access token will be exchanged for a long lived access token.

Links
Code on GitHub

YouTube Video

That is going to do it for this post! Leave any comments/questions/concerns below and thanks for stopping by the blog!

41 comments

  1. 벼룩시장 신문그대로보기 (구인구직, 부동산) 벼룩시장 신문그대로보기 바로가기 그리고 지역별 벼룩시장 종이신문그대로보기 방법 (구인구직, 부동산) 알아볼게요. 교차로신문 같이 벼룩시장은 지역별 일자리, 구인구직, 부동산 등 다양한 정보를 제공해요. 교차로신문그대로보기 바로가기는 아래에서 확인하고, 오늘은 벼룩시장 신문그대로보기 바로가기 그리고 사용법 섹스카지노사이트

  2. Hello! This is my 1st commnent here so I juwt wanted to give a quick
    shout out annd tell yoou I ttuly enjoy redading throujgh your blog posts.
    Can you suggest anyy othedr blogs/websites/forums thaqt deal
    wit the same subjects? Applreciate it!

  3. Its like you read my mind! You appear to understand a lot approximately this, such as you wrote the e book in it or something. I feel that you could do with a few to power the message house a little bit, however other than that, that is great blog. A great read. I will certainly be back.

  4. A person essentially assist to make seriously posts I might state. That is the very first time I frequented your web page and to this point? I surprised with the research you made to make this particular post extraordinary. Great task!

  5. Thank you a bunch for sharing this with all people you actually understand what you’re talking approximately! Bookmarked. Please additionally visit my website =). We can have a hyperlink exchange contract between us!

  6. Its such as you learn my mind! You seem to know so much approximately this, like you wrote the e-book in it or something. I feel that you could do with a few p.c. to pressure the message home a little bit, however instead of that, that is great blog. An excellent read. I’ll certainly be back.

  7. Do you have a spzm issue onn this site; I also am a blogger, annd
    I was wondering yohr situation; wwe have ceated soe
    nice proceduures and wwe are looking tto wap techniques ith other folks,
    please shoot mee an e-mail iif interested.

  8. Thanks for another magnificent post. Where else could anyone get that kind of information in such an ideal way of writing? I’ve a presentation next week, and I am on the look for such information.

  9. Excellent post. I was checkiing continuously tis blpog aand I’m impressed!

    Extremely heppful information specifically tthe las part :
    ) I care for suchh infoo much. I wass loooking for thiss certain informatioon forr a
    verfy long time. Thank yoou andd best of luck.

  10. A few things i have observed in terms of personal computer memory is that often there are specs such as SDRAM, DDR or anything else, that must fit the requirements of the motherboard. If the pc’s motherboard is pretty current and there are no computer OS issues, updating the memory literally will take under one hour. It’s one of the easiest laptop upgrade methods one can visualize. Thanks for spreading your ideas.

  11. Almost all of the things you say happens to be supprisingly precise and it makes me ponder why I had not looked at this with this light previously. This article really did switch the light on for me personally as far as this subject matter goes. However there is one particular issue I am not really too comfortable with and while I attempt to reconcile that with the actual main theme of your point, let me see just what all the rest of the visitors have to say.Very well done.

  12. I have been absent for a while, but now I remember why I used to love this site. Thanks , I’ll try and check back more often. How frequently you update your website?

  13. I want to show thanks to the writer just for rescuing me from this particular challenge. Because of looking throughout the online world and finding tricks which were not helpful, I figured my life was well over. Living minus the solutions to the issues you’ve resolved by means of the article content is a crucial case, as well as the kind that would have negatively damaged my career if I hadn’t encountered your blog post. Your primary talents and kindness in dealing with all the details was excellent. I’m not sure what I would have done if I had not come across such a step like this. I’m able to at this moment look ahead to my future. Thanks for your time so much for this impressive and result oriented guide. I won’t be reluctant to refer your web site to anyone who would like assistance about this topic.

  14. Wow, awesome blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is magnificent, as well as the content!

  15. https://itgunza.com

    https//pornmaster.fun/hd/吉安哪裏有小姐全套包夜服務薇信▷182-888-04吉安哪裏有小姐全套包夜服務吉安哪裏有小姐大保健按摩特殊服務▷吉安哪裏有小姐學生妹過夜上門按摩服務-nip

  16. Thanks for the ideas you are revealing on this site. Another thing I’d really like to say is the fact getting hold of copies of your credit score in order to inspect accuracy of any detail would be the first action you have to conduct in credit repair. You are looking to clean up your credit profile from dangerous details mistakes that mess up your credit score.

  17. I wanted to post you this very small remark to be able to thank you so much as before relating to the great pointers you’ve shared here. It was simply remarkably generous with you to provide freely what most of us could possibly have made available for an e-book to get some cash on their own, primarily considering that you could have tried it in case you desired. These guidelines also served like a fantastic way to understand that other people online have a similar keenness the same as mine to figure out way more with respect to this condition. I am sure there are millions of more pleasant instances up front for people who discover your site.

  18. The following time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my option to read, but I actually thought youd have one thing interesting to say. All I hear is a bunch of whining about one thing that you can fix for those who werent too busy looking for attention.

Leave a Reply

Your email address will not be published. Required fields are marked *