Developer forum

Forum » Templates » Get instagram images in Razor templates?

Get instagram images in Razor templates?

Jacob Storgaard Jensen
Reply

Hi guys, might be a stupid question... But has any of you managed to pull in images from a specific Instagram user into a Razor-template?

I just need to pull in the 9 latest images from an Instagram account, but so much js-crap laying around online that doesn't work anymore... So has anybod managed to do something like that in Razor?


Replies

 
Nicolai Pedersen
Reply

Hi Jacob

Take a look at this: http://www.c-sharpcorner.com/article/how-to-get-instagram-images/

 
Jacob Storgaard Jensen
Reply

Hi Nicolai,
Since that article was written Instagram has changed their API so it requires an accesstoken to be supplied in the "Get" URL or what it's called. This accesstoken shouldn't be exposed in a javascript implementation which is why I would like it to be "hidden" on the server and render the json data to the template...

https://api.instagram.com/v1/users/@userid/media/recent/?access_token=@accesstoken&count=9
(see https://www.instagram.com/developer/endpoints/users/ )

I've tried to use HttpClient and different stuff, but can't see my way to the final result... Earlier on Sten Hougaard helped me with something similar, where we used the old "Indholdsintegration" module to get stuff from Vimeo, but I can't figure out how to change it to work with instagram...

 
Jacob Storgaard Jensen
Reply

Okay, so got it working now, but now I need some kind of caching the json response... Can I somehow cache the json variable?

@using System.Net;
@using Newtonsoft.Json;
@using Newtonsoft.Json.Linq;
@{

  string url = "https://api.instagram.com/v1/users/XXXXXXX/media/recent/?access_token=XXXXXXXXX&count=9";
  dynamic json = "";
  
    WebClient wc = new WebClient();
    json = (JObject)JsonConvert.DeserializeObject(wc.DownloadString(url));
    
    foreach (var data in json.data)
    {
        var imgurl = data.images.standard_resolution.url;
        <img src="@imgurl" />
    }
  
}

 
Nicolai Pedersen
Reply

Yes, here is an example of how to use caching in .NET with expiratin:

http://stackoverflow.com/questions/10989993/cache-expiration

 
Jacob Storgaard Jensen
Reply

Hi Nicolai,

To understand what they write on that page correctly... is caching enabled by default and then I should control how long it should store the cache?

I've been googling for 4 hours now testing everything I could find, but can't get any of it to "play nice" with my code... Any more specific pointers would be deeply appriciated!!

 

You must be logged in to post in the forum