Authorize and Get a Instagram User Token in ASP.NET C#

Introduction

 
For an Instagram login, we have an account, here.
 
Step 1
 
Login/Register in developers.facebook.com
 
Step 2
 
Create an app in developers.facebook.com
 
 
Step 3
 
Give your App Name and Contact Email and click on the "Create App Id" button.
 
Step 4
 
Now we need to add our website in Basic settings. 
 
Settings -> Basic -> Click on "Add Platform" -> select "website" -> give your Site URL.
 
Note
Here, I am giving my asp.net host URL. For this, create an empty asp.net webform application and run it. It will display host information. Copy the URL, then paste it here and click on save changes.
 
 
Step 5
 
Now we need to add Instagram to our App.
 
Click on Products -> set up Instagram.
 
Click on Basic Display -> click on "Create New App" -> give your display name and click on "Create App".
 
 
Now you're able to see your "Instagram App Id", "Instagram App secret".
 
Step 6
 
Now we are registering the  Redirect URL. (create an aspx page in an ASP.NET application) 
 
 
Step 7
 
Now we are adding a test user to our App. Expand Roles -> Roles -> click on "Add Instagram Testers" -> Give your Instagram account ID and click on Submit. Now go to your Instagram account -> Settings -> Security -> Apps and websites. accept the request. Now create an aspx (MyApp.aspx) page in your ASP.NET application.
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="MyApp.aspx.cs" Inherits="InstagramApp.MyApp" %>  
  2.    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
  3.       <div class="jumbotron">  
  4.    <asp:Button ID="btnLogin" runat="server" Text="Log In with Instagram" OnClick="btnLogin_Click"/>  
  5. </div>  
  6. </asp:Content>  
Go to the code behind (MyApp.aspx.cs):
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. namespace InstagramApp {  
  8.     public partial class MyApp: Page {  
  9.         protected void Page_Load(object sender, EventArgs e) {}  
  10.         protected void btnAuthenticate_Click(object sender, EventArgs e) {  
  11.             var instagramAuthApi = "https://api.instagram.com/oauth/authorize";  
  12.             var clientId = "Your client id";  
  13.             var scope = "user_profile,user_media";  
  14.             var responseType = "code";  
  15.             var redirectURL = "https://localhost:12345/OAuthPage.aspx";  
  16.             Response.Redirect(instagramAuthApi + "?client_id=" + clientId + "&redirect_uri=" + redirectURL + "&scope=" + scope + "&response_type=" + responseType);  
  17.         }  
  18.     }  
  19. }  
Now, if you run this page(MyApp.aspx) you will authorize to instagram and it will redirect to the "Redirect URL page" (https://localhost:12345/OAuthPage.aspx).
 
Here we will handle the request in on page load and get our token, here we are using WebClient().
 
OAuthPage.aspx 
  1. <%@ Page Title="OAuthPage" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="OAuthPage.aspx.cs" Inherits="InstagramApp.OAuthPage" %>  
  2.    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
  3.       <div class="jumbotron">  
  4.    
  5.    <asp:Label Visible="false" ID="lblAcessToken" runat="server"></asp:Label>  
  6. </div>  
  7. </asp:Content>  
Go to the code behind OAuth.aspx.cs:
  1. using Newtonsoft.Json;  
  2. using Newtonsoft.Json.Linq;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Collections.Specialized;  
  6. using System.IO;  
  7. using System.Linq;  
  8. using System.Net;  
  9. using System.Web;  
  10. using System.Web.UI;  
  11. using System.Web.UI.WebControls;  
  12. using System.Data;  
  13. using System.Data.SqlClient;  
  14. namespace InstagramApp {  
  15.     public partial class OAuthPage: System.Web.UI.Page {  
  16.         static string code = string.Empty;  
  17.         static string strAccess = string.Empty;  
  18.         protected void Page_Load(object sender, EventArgs e) {  
  19.             if (!String.IsNullOrEmpty(Request["code"]) && !Page.IsPostBack) {  
  20.                 code = Request["code"].ToString();  
  21.                 GetDataInstagramToken();  
  22.             }  
  23.         }  
  24.         public void GetDataInstagramToken() {  
  25.             ClsProperties clsProperties = new ClsProperties();  
  26.             try {  
  27.                 NameValueCollection parameters = new NameValueCollection();  
  28.                 parameters.Add("client_id""Your client id");  
  29.                 parameters.Add("client_secret""Your app secret");  
  30.                 parameters.Add("grant_type""authorization_code");  
  31.                 parameters.Add("redirect_uri""https://localhost:12345/OAuthPage.aspx");  
  32.                 parameters.Add("code", code);  
  33.                 WebClient client = new WebClient();  
  34.                 var result = client.UploadValues("https://api.instagram.com/oauth/access_token""POST", parameters);  
  35.                 var response = System.Text.Encoding.Default.GetString(result);  
  36.                 var jsResult = (JObject) JsonConvert.DeserializeObject(response);  
  37.                 string accessToken = (string) jsResult["access_token"];  
  38.                 lblAcessToken.Visible = true;  
  39.                 lblAcessToken.Text = accessToken;  
  40.             } catch (Exception ex) {  
  41.                 throw new Exception(ex.Message);  
  42.             }  
  43.         }  
  44.     }  
  45. }  
Thanks!