Facebook的连接创建一个新的ASP.NET应用程序

  • 创建一个新的ASP.NET网站
    • . 在Visual Studio 2008,创建一个新的ASP.NET Web应用程序。
    • 右键在解决方案资源管理器并选择属性项目按一下。在Web应用程序属性窗口中,导航到Web选项卡。
    • 验证“使用Visual Studio开发服务器”被选中,并选择“特定端口”单选按钮。 For the port number,
    • 为端口号码,输入48284(时使用的端口创建Facebook应用以上)。保存更改并关闭属性页。注意:IIS也可以用于发展,但简洁的Visual Studio的Web服务器在此使用演练。
  • Add Reference to the Facebook Developer ToolKit 添加引用到Facebook的开发工具包
  • Configuring Cross-Domain Support 配置跨域支持
    • 在Web应用程序进行通信与Facebook的连接平台,跨域通信通道的文件顺序必须创建并添加到Web应用程序域。此文件引用的JavaScript跨域library提供了商标的规定并处理转让与Facebook和第三方的网站。实现很简单:
    • 添加一个新的HTML页面到您的名为“xd_receiver.htm Web应用程序的根”
    • 打开xd_receiver.htm并取代所有与以下标记:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html >
       <body>
          <script src=
           "http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js"
           type="text/javascript"></script>
       </body>
    </html>
     欲了解更多信息,请访问http://wiki.developers.facebook.com/index.php/Cross-domain_communication_channel

 

Facebook的连接组件添加到HTML标记

  • Open Default.aspx. 开放的Default.aspx。
  • Facebook的XML架构参考通过修改<HTML>标记:

    <html xmlns=http://www.w3.org/1999/xhtml xmlns:fb="http://www.facebook.com/2008/fbml"> <html xmlns=http://www.w3.org/1999/xhtml xmlns:fb="http://www.facebook.com/2008/fbml">

  • 参考<HEAD>部分内的商标脚本库:

  • <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"> </ script >

  • 添加到本页面的<BODY> Facebook的登录按钮。

 

  • <fb:login-button onlogin="window.location.reload()"> </布里亚托利:登录的按钮>“

    Note that the onlogin event will trigger the page to reload after Facebook login completes, which will allow the web application to re-examine the authentication status during its Page_Load event to apply authenticated/not authenticated conditional code. 请注意,onlogin事件会触发页面重新载入商标登录完成后,将允许web应用程序重新审查其Page_Load事件中的认证资格申请认证/未通过身份验证条件代码。

  • Add a simple ASP.NET label control to display the current authentication status: 添加一个简单的ASP.NET的标签控制来显示当前的身份验证状态:

    <asp:label id="lblStatus" runat="server" /> <asp:label id="lblStatus" runat="server" />

  • 在最底层的标记,就在结束</网页>标签(添加以下初始化Facebook的JavaScript库:(API密钥更改为您为您的关键在Facebook应用程序生成)

    <script type="text/javascript">
       FB.init("[PASTE API KEY HERE]", "xd_receiver.htm"); 
    </script>

  • 注:其他(非相片)标记已被添加到显示的格式和样本,下载并查看示例源代码,以查看完整的标记。商标绑定到ASP.NET控件对象覆盖更深入以后在这walkthough 。成品抽样标记的形式创建一个能够创造新的相片和展示活动的特邀嘉宾RSVP的地位。

Authentication and Initiating a Session 身份验证和启动一个会话

该示例应用程序现在包含的所有组件,需要沟通与Facebook连接。本节增加了代码隐藏应用程序所需的影响验证身份的转变,创建一个Facebook开发工具包ConnectSession对象和编写工具包的API对象,将用于对商标的REST的API今后所有的来电。

Facebook的连接的地方cookie,包括秘密的会议,会议重点和用户ID后,浏览器的用户进行身份验证。Facebook的开发工具包中的ConnectSession类处理这些cookie值检索和管理的格式提交给Facebook平台这些值。

For more information on Facebook Connect authentication, see http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works . 欲了解更多关于Facebook的连接的身份验证信息,见

http://www.my400800.cn

http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works

Adding Code-Behind Logic 添加代码背后的逻辑

  • 打开Default.aspx.cs代码隐藏类。
  • 添加私有成员来存储API密钥与应用秘密,会议和Facebook的连接工具包的对象( 注意,这只是演示的目的是 ,生产应用程序的web.config应该存储在这些键,或使用其他手段,确保了 )。 A generic List of the custom type EventUser is declared to store a collection of users for an existing Event. 一个自定义类型EventUser通用存储名单宣布为现有的用户事件的集合。

    private const string ApplicationKey = "[Application Key]";
    private const string SecretKey = "[Application Secret Key]";
    private Api _facebookAPI;
    private ConnectSession _connectSession;
    private List<EventUser> _eventUsers;

  • 编辑Page_Load事件处理程序如下。阿ConnectSession对象是实例化在每个负载的应用中的 关键和秘密密钥(也可以通过这个存储,可重复使用的对象)。ConnectSession.IsConnected方法的研究和指导代码流向适当的行为: 如果没有连接,页面只显示登录信息。如经确认,一个Facebook开发工具包API对象初始化使用ConnectSession实例。用户检索的对象是 目前已全面启用发出呼吁对Facebook的空气污染指数。 Finally, the status is updated with a simple message showing we have authenticated the user. 最后,状态更新,显示我们有一个简单的验证用户信息。
protected void Page_Load(object sender, EventArgs e)
         {
            // Authenticated, created session and API object
            _connectSession = new ConnectSession(APPLICATION_KEY, SECRET_KEY);

            if (!_connectSession.IsConnected())
            {
                // Not authenticated, proceed as usual.
                lblStatus.Text = "Please sign-in with Facebook.";
            }
            else
            {
                // Authenticated, create API instance
                _facebookAPI = new Api(_connectSession);

                // Load user
                user user = _facebookAPI.Users.GetInfo();

                if (!Page.IsPostBack)
                {
        // Display the user’s name
                    lblStatus.Text = string.Format("Signed in as {0} {1}", 
                        user.first_name, user.last_name);
                    

                    // Set the NavigateUrl target address for granting this application
  // the create_event extended permission
                    NewEventGrantPermission.NavigateUrl =   
                        string.Format("http://www.facebook.com
                                      /authorize.php?api_key={0}&v=1.0
                                      &ext_perm={1}", 
                                      APPLICATION_KEY,
                                      Enums.ExtendedPermissions.create_event);

  // Set dummy event date
                    NewEventDate.Text = DateTime.Now.AddDays(7).ToString();
                    

  // Set a default host name (user’s name) 
  NewEventHost.Text = string.Format("{0} {1}", 
                                        user.first_name, user.last_name);

                    // Load existing Facebook Events
                    LoadExistingEvents();
                }
            }

            if (!Page.IsPostBack)
            {
                // Set section visibility
                ToggleSectionVisibility();
            }
         }

 

使用Facebook的工具包来访问API的方法

 

acebook的开发工具包(FDT的)的建立是为了减轻使用Facebook的API和开发平台。NET开发人员。一个Facebook的API的方法的列表,可以在发现http://wiki.developers.facebook.com/index.php/空气污染指数 。FDT的包装了休息的功能的,配料,数据存储,Facebook的连接和权限的API。

例如,要检索当前用户的活动并将其绑定到一个ASP.NET控制,使用的FacebookAPI对象的事件的 实例来调用该API的method calls available在这种情况下(一身,Events.Get用于为了检索从Facebook的API事件对象的集合)。

 

示例1:通过Facebook检索和开发工具包绑定事件

 

// Get user's events via Facebook Toolkit API object
var facebookevents = _facebookAPI.Events.Get();

// Bind list to existing events
EventList.DataSource = facebookevents;

// Set the display and value fields
EventList.DataTextField = "name";
EventList.DataValueField = "eid";

// Bind the data to control
EventList.DataBind();
 

 

例2:在复杂ASP.NET控件绑定对象

一个稍微复杂的例子演示了如何绑定FDT的结果对象到ASP.NET ListView控件。

T 该LoadEventMembers方法检索所选事件的现有活动 下拉的ID,并调用FDT的方法Events.GetMembers返回指定事件一EventMemberInfo实例包含的RSVP列出了用户名()。 用户当前的事件列表被重置,并为每个RSVP的地位,LoadUsers被调用。LoadUsers方法简单的问题对于用户的ID,这反过来又填充了我们 的定制EventUser对象(实例只用于绑定到当前列表中的ListView使用一个请求Users.GetInfo)。 A simple sort is performed by last name, and the List is bound to the ListView ASP.NET control: 一个简单的排序是按姓氏进行,而且将会有一定的ListView ASP.NET控件:

private void LoadEventMembers()
{
// Get the currently selected Event ID
      long eid;
      if (!long.TryParse(EventList.SelectedValue, out eid)) return;
      if(eid <= 0) return;

// Reset
      var eventMemberInfo = _facebookAPI.Events.GetMembers(eid);

      // Reset event users
      _eventUsers = new List<EventUser>();

      // Load for each response type
      LoadUsers(eventMemberInfo.attending.uid, EventUser.EventUserStatus.Attending);
      LoadUsers(eventMemberInfo.declined.uid, EventUser.EventUserStatus.Declined);
      LoadUsers(eventMemberInfo.not_replied.uid, EventUser.EventUserStatus.NoResponse);
      LoadUsers(eventMemberInfo.unsure.uid, EventUser.EventUserStatus.Undecided);

      // Sort by last name
      _eventUsers.Sort((x, y) => x.LastName.CompareTo(y.LastName));

      ExistingEventFriendsList.DataSource = _eventUsers;
      ExistingEventFriendsList.DataBind();
}

private void LoadUsers(List<long> uids, EventUser.EventUserStatus eventUserStatus)
{
var users = _facebookAPI.Users.GetInfo(uids);
      foreach(var u in users)
      {
      	_eventUsers.Add(new EventUser(u, eventUserStatus));
      }
}

 

标记下面演示了如何绑定到我们的自定义对象的ListView和呈现我们的自定义表的布局:

<asp:ListView ID="ExistingEventFriendsList" runat="server" DataKeyNames="UserID">
  <LayoutTemplate>
    <table cellpadding="2" border="1" style="margin:6px 0 0 0;">
      <tr id="Tr1" runat="server">
        <th id="Th1" runat="server"></th>
        <th id="Th2" runat="server"></th>
        <th id="Th3" class="ExistingEventResponseCell" runat="server">Attending</th>
        <th id="Th4" class="ExistingEventResponseCell" runat="server">No Response</th>
        <th id="Th5" class="ExistingEventResponseCell" runat="server">Undecided</th>
        <th id="Th6" class="ExistingEventResponseCell" runat="server">Declined</th>
      </tr>
      <tr runat="server" id="itemPlaceholder" />
    </table>
  </LayoutTemplate>
  <ItemTemplate>
  <tr>
    <td>
      <img src="<%# ((EventUser)Container.DataItem).PicSquare %>" height="30px"
           width="30px" alt="profile pic" />
    </td>
    <td valign="top">
      <asp:Label ID="LastNameLabel" runat="Server" Text='<%# string.Format("{0} {1}",
                                   ((EventUser)Container.DataItem).FirstName,
                                   ((EventUser)Container.DataItem).LastName) %>' />
    </td>                    
    <td class="ExistingEventResponseCell">
      <asp:CheckBox ID="Attending" runat="Server" Enabled="false" 
                    Checked='<%# ((EventUser)Container.DataItem).IsAttending %>' />
    </td> 
    <td class="ExistingEventResponseCell">
      <asp:CheckBox ID="NoResponse" runat="Server" Enabled="false" 
                      Checked='<%# ((EventUser)Container.DataItem).IsNotResponded %>' />
    </td> 
    <td class="ExistingEventResponseCell">
    	<asp:CheckBox ID="Undecided" runat="Server" Enabled="false" 
              Checked='<%# ((EventUser)Container.DataItem).IsUnsure %>' />
    </td> 
    <td class="ExistingEventResponseCell">
      <asp:CheckBox ID="Declined" runat="Server" Enabled="false" 
              Checked='<%# ((EventUser)Container.DataItem).IsDeclined %>' />
    </td>                    
  </tr>
  </ItemTemplate>  
</asp:ListView>

 对于每一个事件被邀请者,一个记录将被添加到ListView中与用户的RSVP的状况:

 

扩展权限是自愿选择具体的行动要求,从当前用户。这些方法需要更多的来自用户的信任水平,必须以个人的方式批出。推广的权限是:

  • Public_stream Public_stream
  • Read_stream Read_stream
  • Email 电邮
  • Offline_access Offline_access
  • Create_event Create_event
  • RSVP_event RSVP_event
  • SMS 短信
  • Status_update Status_update
  • Photo_upload Photo_upload
  • Video_upload Video_upload
  • Create_Note Create_Note
  • Share_Item Share_Item

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值