Get Instagram Media Details in ASP.NET

Introduction 

 
To get Instagram Media details, you need a developer account and accesstoken.
 
Please see my earlier post on how to get an accesstoken from Instagram. Here is the blog.
 
Step 1
 
Log in into a developer account, get an AccessToken, and preserve it.
 
Step 2
 
Create an ASP.Net web application and add a page (PostDetails.aspx) (or) to continue with my earlier post, add a new aspx page.
 
Step 3
 
Create a button(GetInstaPostDetails) in aspx page.
 
Also, add GridView on the page to display details.
 
Code in PostDetails.aspx:
  1. <%@ Page Title="PostDetails" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="PostDetails.aspx.cs" Inherits="InstagramApp.PostDetails" %>  
  2.    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
  3.       <div class="jumbotron">  
  4.       <asp:Button ID="btnInstUserMedia" runat="server" Text="GetPostDetails" OnClick="btnInstUserMedia_Click" />  
  5.       <br />  
  6.       <br />  
  7.    <asp:GridView ID="GridViewPostCaption" runat="server"></asp:GridView>  
  8.  </div>  
  9. </asp:Content>  
Step 4
 
Go to the code behind (PostDetails.aspx.cs):
  1. using Newtonsoft.Json.Linq;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Net;  
  8. using System.Web;  
  9. using System.Web.UI;  
  10. using System.Web.UI.WebControls;  
  11. namespace InstagramApp {  
  12.     public partial class PostDetails: System.Web.UI.Page {  
  13.         protected void Page_Load(object sender, EventArgs e) {}  
  14.         protected void btnInstUserMedia_Click(object sender, EventArgs e) {  
  15.             string id = string.Empty;  
  16.             string caption = string.Empty;  
  17.             string mediaType = string.Empty;  
  18.             string mediaUrl = string.Empty;  
  19.             string strAccessToken = "Your Access Token";  
  20.             string instaGraphURL = "https://graph.instagram.com/";  
  21.             string instMediaURL = instaGraphURL + "me/media?fields=id,caption,media_type,media_url&access_token=" + strAccessToken;  
  22.             System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);  
  23.             try {  
  24.                 HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(instMediaURL);  
  25.                 httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;  
  26.                 using(HttpWebResponse response = (HttpWebResponse) httpWebRequest.GetResponse())  
  27.                 using(Stream stream = response.GetResponseStream())  
  28.                 using(StreamReader reader = new StreamReader(stream)) {  
  29.                     var obj = reader.ReadToEnd();  
  30.                     JObject userObject = JObject.Parse(obj);  
  31.                     foreach(var result in userObject["data"]) {  
  32.                         id = (string) result["id"];  
  33.                         caption = (string) result["caption"];  
  34.                         mediaType = (string) result["media_type"];  
  35.                         mediaUrl = (string) result["media_url"];  
  36.                     }  
  37.                     DataTable dt = new DataTable();  
  38.                     dt.Columns.Add("Id"typeof(string));  
  39.                     dt.Columns.Add("Caption"typeof(string));  
  40.                     dt.Columns.Add("MediaType"typeof(string));  
  41.                     dt.Columns.Add("MediaUrl"typeof(string));  
  42.                     dt.Rows.Add(id, caption, mediaType, mediaUrl);  
  43.                     GridViewPostDetails.DataSource = dt;  
  44.                     GridViewPostDetails.DataBind();  
  45.                 }  
  46.             } catch (Exception ex) {  
  47.                 throw new Exception(ex.Message);  
  48.             }  
  49.         }  
  50.         private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) {  
  51.             return true;  
  52.         }  
  53.     }  
  54. }  
Step 5
 
NameSpaces need to include the following:
  1. using System.Net;  
  2. using System.IO;  
  3. using Newtonsoft.Json.Linq;  
  4. using System.Data;   
Now you will get Instagram Post details.
 
Thank you.