Monday, May 17, 2010

Working code for facebook new api login and stream publish C#

Working code for facebook login and stream publish C#
Errors:-
1. parameter uid or session key required
2.Permissions error
 
 
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head id="Head1" runat="server">
    <title></title>
    <script language=javascript>
        function connectLogin() {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm._doPostBack('UpdatePanel1', '');
        }
        setTimeout("connectLogin()", 10000);
    </script>
</head>
<body>
    <form id="form1" runat="server">
       <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Label ID="lblStatus" runat="server" Text="Label"></asp:Label>
<asp:Literal ID="connectLoginControl" runat=server><fb:login-button onlogin=connectLogin() ></fb:login-button></asp:Literal>
            </ContentTemplate>
        </asp:UpdatePanel>
           
    </div>
    </form>
    <script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"></script>
    <script type="text/javascript">
        FB.init("Your Api", "xd_receiver.htm");
      
       
    </script>
   
    <input id="Button2" type="button" value="button" onclick=connectLogin() />
</body>
</html>
Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Rest;
using Facebook.Schema;
using Facebook.Session;
using Facebook.Utility;

using Facebook;

using Facebook.Session.DesktopPopup;

public partial class Default2 : System.Web.UI.Page
{
    private const string APPLICATION_KEY = "you Api Key";
    private const string SECRET_KEY = "Your secret";

    private Api _facebookAPI;
    // private List<EventUser> _eventUsers;
    //private ConnectSession _connectSession;

    private Facebook.Rest.Api Api;
    private Facebook.Session.ConnectSession connectSession;
    protected void Page_Load(object sender, EventArgs e)
    {
       // Response.Redirect("http://www.facebook.com/login.php?api_key=you Api Key&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&req_perms=offline_access");
          
        connectSession = new  Facebook.Session.ConnectSession(APPLICATION_KEY, SECRET_KEY);
        if (!connectSession.IsConnected())
        {
            lblStatus.Text = "Please sign-in with Facebook." + DateTime.Now.ToString();
        }
        else
        {
            Api = new Facebook.Rest.Api(connectSession);
            Facebook.Schema.user u = Api.Users.GetInfo();
           
            Session["facebook_userId"] = Api.Session.UserId;
            lblStatus.Text = string.Format("Welcome, " + u.name +"  :-" +Session["facebook_userId"]);

            bool hasPublishStream = Api.Users.HasAppPermission(Enums.ExtendedPermissions.publish_stream, Convert.ToInt64(Api.Users.GetLoggedInUser()));
            bool hasOffline = Api.Users.HasAppPermission(Enums.ExtendedPermissions.offline_access, Convert.ToInt64(Api.Users.GetLoggedInUser()));

           
            Response.Write("test permission "+hasPublishStream);
            //Api.Stream.Publish("Test Messages", null, null, null, Api.Users.GetLoggedInUser());
            Api.Stream.Publish("Test Messages", null, null, Api.Users.GetLoggedInUser().ToString(),0);


            connectLoginControl.Visible = false;

        }


    }


}

1 comment:

Arsen Yeremin said...

Did you figure it out?