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:
- <%@ Page Title="PostDetails" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="PostDetails.aspx.cs" Inherits="InstagramApp.PostDetails" %>
- <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
- <div class="jumbotron">
- <asp:Button ID="btnInstUserMedia" runat="server" Text="GetPostDetails" OnClick="btnInstUserMedia_Click" />
- <br />
- <br />
- <asp:GridView ID="GridViewPostCaption" runat="server"></asp:GridView>
- </div>
- </asp:Content>
Step 4
Go to the code behind (PostDetails.aspx.cs):
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace InstagramApp {
- public partial class PostDetails: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {}
- protected void btnInstUserMedia_Click(object sender, EventArgs e) {
- string id = string.Empty;
- string caption = string.Empty;
- string mediaType = string.Empty;
- string mediaUrl = string.Empty;
- string strAccessToken = "Your Access Token";
- string instaGraphURL = "https://graph.instagram.com/";
- string instMediaURL = instaGraphURL + "me/media?fields=id,caption,media_type,media_url&access_token=" + strAccessToken;
- System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
- try {
- HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(instMediaURL);
- httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
- using(HttpWebResponse response = (HttpWebResponse) httpWebRequest.GetResponse())
- using(Stream stream = response.GetResponseStream())
- using(StreamReader reader = new StreamReader(stream)) {
- var obj = reader.ReadToEnd();
- JObject userObject = JObject.Parse(obj);
- foreach(var result in userObject["data"]) {
- id = (string) result["id"];
- caption = (string) result["caption"];
- mediaType = (string) result["media_type"];
- mediaUrl = (string) result["media_url"];
- }
- DataTable dt = new DataTable();
- dt.Columns.Add("Id", typeof(string));
- dt.Columns.Add("Caption", typeof(string));
- dt.Columns.Add("MediaType", typeof(string));
- dt.Columns.Add("MediaUrl", typeof(string));
- dt.Rows.Add(id, caption, mediaType, mediaUrl);
- GridViewPostDetails.DataSource = dt;
- GridViewPostDetails.DataBind();
- }
- } catch (Exception ex) {
- throw new Exception(ex.Message);
- }
- }
- private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) {
- return true;
- }
- }
- }
Step 5
NameSpaces need to include the following:
- using System.Net;
- using System.IO;
- using Newtonsoft.Json.Linq;
- using System.Data;
Now you will get Instagram Post details.
Thank you.