Tuesday, 28 February 2017


Facebook Sharing in ASP .Net

Code-Behind

using Facebook;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Http;



namespace FacebookDev
{
    public partial class FacebookSharing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               // Authorization();
            }
        }
        protected void Authorization()
        {
            string app_id = "982579805207954";
            string app_sceret = "d8d9b44d566ef09132a054b119b7821d";
            string scope = "publish_actions,manage_pages";
            if (Request["code"] == null)
            {
                Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                  app_id, Request.Url.AbsoluteUri, scope));
            }
            else
            {
                Dictionary<string, string> tokens = new Dictionary<string, string>();
                string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
                   app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_sceret);

             
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
           
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    StreamReader responseReader = null;
                    string responseData = "";

                    responseReader = new StreamReader(response.GetResponseStream());

                    responseData = responseReader.ReadToEnd();
                 
                    foreach (string token in responseData.Split('&'))
                    {
                        //meh.aspx?token1=aa&token2=bb&...
                        tokens.Add(token.Substring(0, token.IndexOf("=")),
                            token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
                    }
                }
                string access_token = tokens["access_token"];
                var client = new FacebookClient(access_token);
                client.PostTaskAsync("/me/feed", new { message = txtShow.Text });
            }
        }

        protected void share_button_Click(object sender, EventArgs e)
        {
            Authorization();
        }
    }
}

========================================================================
Design Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FacebookSharing.aspx.cs" Inherits="FacebookDev.FacebookSharing" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script>
    window.fbAsyncInit = function () {
        FB.init({
            appId: '1492479487428964', status: true, cookie: true,
            xfbml: true
        });
    };
    (function () {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
        'https://connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());
</script>
<script type="text/javascript">
    $(document).ready(function () {
        //$('#share_button').click(function (e) {
            e.preventDefault();
            FB.ui(
{
    method: 'feed',
   // name: TestVar,
    link: 'http://localhost:1536/FacebookSharing.aspx',
    //picture: 'http://www.c-sharpcorner.com/UploadFile/AuthorImage/raj1979.gif',
    caption: 'This article demonstrates how to use the jQuery dialog feature in ASP.Net.',
    description: 'First of all make a new website in ASP.Net and add a new stylesheet and add .js files and put images in the images folder and make a reference to the page.',
    //message: 'hello raj message'
});
        });
    //});
</script>
<body>
    <script>
        window.fbAsyncInit = function () {
            FB.init({
                appId: '1492479487428964',
                xfbml: true,
                version: 'v2.8'
            });
            FB.AppEvents.logPageView();
        };

        (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement(s); js.id = id;
            js.src = "https://connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>
    <form id="form1" runat="server">

        <div align="center">
            <div id="fb-root">
            </div>
            <div>
                <asp:TextBox ID="txtShow" runat="server" TextMode="MultiLine" Height="60px"></asp:TextBox>
            </div>
            <div></div>
            <div>
                <asp:Button runat="server" ID="share_button" Text="Share" OnClick="share_button_Click" />
            </div>
        </div>
    </form>
</body>
</html>



No comments:

Post a Comment