Monday, November 23, 2009

Set Facebook status message for offline user with c#

Your Requirement:
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 with the following parameters. You need to change APIKey, Secret and Callback with the actual values from Facebook application settings.

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>);
}
}
}

Sunday, November 8, 2009

Playing Farmville like a Pro

If you are a farmville fan, this is the right place for you. Here I show you how you can play this game very effectively. This includes some highly-rewarding hacks that not only save your game time but also increase your levels pretty fast than normally possible.

What you need? A PC with windows OS, Mozilla Firefox browser and a curious mind (a wicked desire to win your friends is a huge plus point :D).

Lets get started. I don't want to repeat what's already been discovered and floating around the net. However, I will show you the links because these things must be known before getting started.

First, it is really important to decrease the load time for the flash component.
  1. Disable High Quality by clicking on eye symbol near your current gold (Top-Left corner).
  2. Install "Greasemonkey" firefox plugin from here. After that, install the Greasemonkey scripts Farmville view simplifier and Farmville Time of Harvest.
[Technical Details: Greasemonkey is a cool firefox plugin that allows any html content to be pre-processed with given javascript script before being rendered. "Farmville view simplifier" removes the Ads from your farmville page and it loads a little faster. "Farmville Time of Harvest" adds a cool button at the top-left, "FvCalc" where you can click and find out when will your current crop be exactly harvested.]

Now that your farm loads faster, I will show you how to perform actions fast (faster than manually, that is :D).

Get a copy of GridClicker from here. It should be pretty obvious how to use it, however if you get confused, just go through the video on the same link. This tool allows you to plough, plant and harvest with minimal intervention. (I really hate the way Zinga made farmville UI. Clicking the whole farm plots over and over was very boring and tedious). Someone had to come up with tools like this. A big thank you from my side to the glsoft developer who made this wonderful tool.

Lastly, my only suggestion is: plant any crop that you think is profitable, save as much money as you can and apply the famous "Soyabean-delete" strategy using GridClicker tool. If you need advanced tips I recommend you this. But I personally think its not mandatory to know each detail. The only important thing is to use Soyabean-delete method to convert your extra money to experience. With this method, you can get 1 experience point per 10 gold coins. This is not possible with the purchase of any item to my knowledge (unless you are targeting ribbons).

Using this method you should be easily gaining more than 10,000 experience points per day!

Lastly, if you need many Farmville friends, just add as many as you desire from this thread.