{"id":3193,"date":"2022-04-28T16:38:57","date_gmt":"2022-04-28T15:38:57","guid":{"rendered":"https:\/\/www.simonbattersby.com\/blog\/?p=3193"},"modified":"2022-04-28T16:41:42","modified_gmt":"2022-04-28T15:41:42","slug":"loading-instagram-posts-on-your-website-using-powershell-with-curl","status":"publish","type":"post","link":"https:\/\/www.simonbattersby.com\/blog\/2022\/04\/loading-instagram-posts-on-your-website-using-powershell-with-curl\/","title":{"rendered":"Loading Instagram posts on your website &#8211; using Powershell with cURL"},"content":{"rendered":"<p>Spent some time yesterday including Instagram posts on one of my websites (<a href=\"https:\/\/www.adelaidewalker.co.uk\/\">this one<\/a> if you&#8217;re interested). In order to get this going you need to use cURL, run from a command line, which was new to me. Windows 10 and later allows you to do this from Powershell, but I had to mess around a bit before it works, so here goes if anyone else needs this.<\/p>\r\n\r\n<p>To start off you need to create an Instagram App. The Instagram instructions <a href=\"https:\/\/developers.facebook.com\/docs\/instagram-basic-display-api\/getting-started\">here<\/a> are fine, so I won&#8217;t go through that.<\/p>\r\n\r\n<p>My trouble started at Step 5 &#8211; &#8216;Exchange the Code for a Token&#8217;. Instagram gives this example code:<\/p>\r\n\r\n<pre>curl -X POST \\\r\n  http:\/\/api.instagram.com\/oauth\/access_token \\\r\n  -F client_id=684477648739411 \\\r\n  -F client_secret=eb8c7... \\\r\n  -F grant_type=authorization_code \\\r\n  -F redirect_uri=http:\/\/yoursite.com\/auth\/ \\\r\n  -F code=AQDp3TtBQQ...<\/pre>\r\n\r\n<p>&#8230;but this needs amending to use in Powershell, to:<\/p>\r\n\r\n<pre>curl.exe  '-X'  'POST' 'http:\/\/api.instagram.com\/oauth\/access_token' '-F' 'client_id=684477648739411' '-F' 'client_secret=eb8c713c492d1f9c19d7120e94ffcb205' '-F' 'grant_type=authorization_code' '-F' 'redirect_uri=http:\/\/www.yoursite.com\/' '-F' 'code=AQDp3TtBQQh297S2as5YbDNAU9h3772mvryptCeWWT28z-Xadqq4YfA_YiGXui-cNnLjT7Wg_mrC0OAe0Q5l5x1RvdMarGvFIC1fv7I_kz32oGfKn-R1YlTEyLnPCKksGHeVgw7aJ3iLgXaS8QB9q-WNdG8PiWICz5Icwb4XULBNXs9DONlkIA1i94lNXoKsnkbLL13aMJLGpiiMksitRuZSRblwKrI1dEi7xqeHPcd9VJQ'<\/pre>\r\n\r\n<p>This took me about an hour to work out&#8230; Similarly, the request for a long-lived token needs to be formatted like this:<\/p>\r\n\r\n<pre>curl.exe '-i' '-X' 'GET' 'http:\/\/graph.instagram.com\/access_token?grant_type=ig_exchange_token&client_secret=eb8c713c492d1f9c19d7120e94ffcb205&access_token=IGQVJYclF4WUllREZABbU9rWm1TS2F0am12cWRUVWxIbURLQXc2eXZAyaTVmNnU5a0ZAFTlU4bkFCOG5sd0JwWFMwZA0VYR0ZAFcnVBZAzZAoVFFwdVN5bmVaWmNGQm42X3VvSEJNc0ZAfWG1zbjB3OThFbm9SNk1wZAHZArOVJVQnQw'<\/pre>\r\n\r\n<p>Once you&#8217;ve got the access token then you&#8217;re off and running. I was using PHP to call the Instagram data from Opencart, so I created a custom module, stored the access token as an Opencart setting and included this code to call Instagram (in a model file, called by the module controller):<\/p>\r\n\r\n<pre>\tpublic function getInstagramContents() {\r\n\t\t$fields = \"media_type,media_url,permalink,thumbnail_url\";<span class=\"code_comment\">\/\/call the fields you want\r\n<\/span>\t\t$token = $this-&gt;config-&gt;get('instagram_access_token');<span class=\"code_comment\">\/\/get the stored token\r\n<\/span>\t\t$limit = 4;<span class=\"code_comment\">\/\/get the four most recent posts\r\n<\/span>\t\t \r\n\t\t$json_feed_url=\"http:\/\/graph.instagram.com\/me\/media?fields={$fields}&access_token={$token}&limit={$limit}\";\r\n\t\t$json_feed = @file_get_contents($json_feed_url);\r\n\t\t$contents = json_decode($json_feed, true, 512, JSON_BIGINT_AS_STRING);\r\n\t\t\r\n\t\treturn $contents;\r\n\t}<\/pre>\r\n\r\n<p>If you&#8217;ve got this far you&#8217;re probably OK with the restof it, but if the rest of the Opencart stuff is useful let me know in the contents below.<\/p>\r\n\r\n\r\n","protected":false},"excerpt":{"rendered":"<p>Spent some time yesterday including Instagram posts on one of my websites (this one if you&#8217;re interested). In order to get this going you need to use cURL, run from a command line, which was new to me. Windows 10 and later allows you to do this from Powershell, but I had to mess around [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[38,28,39],"class_list":["post-3193","post","type-post","status-publish","format-standard","hentry","category-web-design-and-build","tag-instagram","tag-opencart","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/3193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=3193"}],"version-history":[{"count":4,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/3193\/revisions"}],"predecessor-version":[{"id":3197,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/3193\/revisions\/3197"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=3193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/categories?post=3193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/tags?post=3193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}