Set a status message for a user from a background job or windows service.
Pre-requisite:
Facebook API Toolkit for ASP.net (facebook.dll, facebook.web.dll, Microsoft.Xml.Schema.Linq.dll) - freely available.
You should have an iFrame application registered with facebook.
Background:
The task contains two parts. First, you should ask the user's permission to publish on his behalf and when the user is offline. Next, comes actual posting of the message.
Getting user's permission:
Step 1. Create a new ASP.net project
Step 2. In the web.config file, replace
File: web.config
<appSettings>
<add key="APIKey" value="4a4adf9a0f4657c94f13de3b200cab01"/>
<add key="Secret" value="0d0814aab7b4af8da104f1c5af9c16a6"/>
<add key="Callback" value="http://localhost:3451/"/>
<add key="Suffix" value="TestApp"/>
<add key="TemplateID1" value="20889707566"/>
<add key="TemplateID2" value="20889902566"/>
</appSettings>
Step 3. In the Default.aspx file, write the following code
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="YourNamespace._Default" %>
<%@ Register Assembly="facebook.web" Namespace="facebook.web" TagPrefix="cc1" %>
<%@ MasterType VirtualPath="~/Site.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="Server">
<h1>Getting User Permissions</h1>
</asp:Content>
Step 4. Add Site.master file to the project with the following content
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="YourNamespace.Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<link rel="Stylesheet" type="text/css" href="/css/fbstyle.css" />
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<asp:ContentPlaceHolder ID="header" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<div style="float:right"><a style="border: none" href="http://feeds.stevetrefethen.com/SteveTrefethensWeblog"></a><img alt="RSS Feed" src="http://www.stevetrefethen.com/blog/images/feed-button-32x32.png" /></div>
<div style="clear:right"></div>
<!-- Support dynamic resizing of iframe details here: http://wiki.developers.facebook.com/index.php/Resizable_IFrame -->
<div id="FB_HiddenIFrameContainer" style="display:none; position:absolute; left:-100px; top:-100px; width:0px; height: 0px;"></div>
<script type="text/javascript">
function FB_test_perm(api_key, xd_receiver, permission, callback) {
FB.Bootstrap.requireFeatures(["Connect"], function() {
FB.Facebook.init(api_key, xd_receiver);
FB.Connect.requireSession(function() {
FB.Facebook.apiClient.users_hasAppPermission(permission, function(result) {
if (result != 0) {
// was already granted
return callback(false);
}
FB.Connect.showPermissionDialog(permission, function(result) {
return callback(result == null ? false : (result.indexOf(permission) != -1));
}, true, null);
});
});
});
}
function FB_perm_result(isGranted) {
if (isGranted) {
window.location.reload();
} else {
}
}
FB_test_perm('<% =System.Configuration.ConfigurationSettings.AppSettings["APIKey"] %>',
"xd_receiver.htm", "publish_stream", FB_perm_result);
FB_test_perm('<% =System.Configuration.ConfigurationSettings.AppSettings["APIKey"] %>',
"xd_receiver.htm", "offline_access", FB_perm_result);
</script>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="MainContentPlaceHolder" runat="server"></asp:contentplaceholder>
</div>
</form>
<asp:ContentPlaceHolder id="footer" runat="server"></asp:ContentPlaceHolder>
</body>
</html>
Step 5. Add a cross-domain receiver file xd_receiver.htm with the following content
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cross-Domain Receiver Page</title>
</head>
<body>
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/XdCommReceiver.debug.js" type="text/javascript"></script>
</body>
</html>
And... that's it! Just compile and run the application. This should open up your application in iFrame and ask for publish_stream and offline_access permissions. Allow the permissions and proceed with the next phase.
One more thing. Save the uid of user (got from Master.API.uid) and session key (got from Master.API.SessionKey). We'll need this later.
Step 6. Posting status updates when user is offline.
Create a new project which can be any of desktop/web-based/windows service application.
Write the following code and execute!
using ...
using facebook;
using facebook.web;
namespace TestApp
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
facebook.API api = new API();
api.ApplicationKey = <same as your appkey in web.config>
api.Secret = <same as your app secret in web.config>
api.SessionKey = <the session key you've saved for a user>
api.uid = <the uid of user>
api.users.setStatus("My first status update", <uid of user>);
}
}
}
No comments:
Post a Comment