I'm currently messing around with the Facebook API. I created a Facebook app and everything. Then (with the help of XhacK) I managed to post to my wall and some of my friends' walls using the Facebook C# SDK. Then I found out that I cannot use the Request dialog using the C# SDK because facebook does not support that :/

So I had to use the Javascript API and I think things went okey... (I managed to send out invites). Here's what I got so far:
[URL=#]Send Application Request[/URL]
<div id="fb-root"></div>
<script type="text/javascript">
    window.fbAsyncInit = function() {
        FB.init({
          appId   : '193005690721590',
          status  : true,
          cookie  : true,
          xfbml   : true
        });
    };
 
    $('a').click(sendRequest);
    function sendRequest() {
        FB.ui({
            method: 'apprequests',
            message: 'Invitation to the test application!',
            title: 'Send your friends an application request',
        },
        function (response) {
            if (response && response.request_ids) {
                var requests = response.request_ids.join(',');
                console.log(requests); //I got some large number... What is that exactly?
            } else {
                alert('canceled');
            }
        });
        return false;
    }
 
    (function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
    }());
</script>
But then when someone tries to accept the invitation, they end up getting a 404 - Unable to Connect error. Is that because I haven't submitted the app to facebook yet, or is there something wrong with my code?

P.S: What I need to do eventually is to have the user invite their friends to the FB app which will then redirect them to the registration page on the website. Also, I need to pass some "invitation code" for tracking purposes (who actually invited that person?).

Any help would be really appreciated.

I'm totally new to this but I finally started to get the hang of it :)
Hey Kassem, I have been meaning to write a Facebook app for a while, but got so confused on where to start. It would be great if you (or Xhack, who apparently is an expert in the matter), could create a small tutorial on how to get started, as well as an intro to the API.

Whoever does that, I will love for ever (and buy a beer when I get home)
Hey Kassem,
you can send data with the invitation (invitation code).
When the user clicks on accept it will redirect to the canvas URL of your app.
I am sure you already read http://developers.facebook.com/docs/reference/dialogs/requests/

I really heavily on requests to invite people to apps I do.
here is a small sample:
http://www.facebook.com/BeirutDutyFree?sk=app_199781310052427
click on the top share button.

I do not send any data with the request though but according to the documentation it should be straight forward.

In addition, do a lot of testing, there are a lot of bugs and wrong docs for facebook API, they roll out features that are not tested every day.
rahmu wroteHey Kassem, I have been meaning to write a Facebook app for a while, but got so confused on where to start. It would be great if you (or Xhack, who apparently is an expert in the matter), could create a small tutorial on how to get started, as well as an intro to the API.

Whoever does that, I will love for ever (and buy a beer when I get home)
I got very confused myself as well. But apparently, once you get the authentication right, everything else should be relatively simple and depends a lot on your creative mind. The API is heavily documenting but they add new things and change things around very frequently, which is not good. You should check out XhacK's blog, he got a collection of tutorials there that could be very helpful.

Good luck :)
ZeRaW wroteHey Kassem,
you can send data with the invitation (invitation code).
When the user clicks on accept it will redirect to the canvas URL of your app.
I am sure you already read http://developers.facebook.com/docs/reference/dialogs/requests/

I really heavily on requests to invite people to apps I do.
here is a small sample:
http://www.facebook.com/BeirutDutyFree?sk=app_199781310052427
click on the top share button.

I do not send any data with the request though but according to the documentation it should be straight forward.

In addition, do a lot of testing, there are a lot of bugs and wrong docs for facebook API, they roll out features that are not tested every day.
Yes I understand that. Using the 'data' optional property I could pass whatever I want. But I'm having trouble dealing with the returned result. Moreover, I want to know why I'm getting the 404 error when someone accepts the invitation, is it because the application hasn't been submitted yet?
Ok I know why it's throwing a 404 error, that is because the canvas url is localhost :P but anyway, how in the hell am I supposed to test whether this works or not?!
Kassem,

Did you try to track the requests (as a client) from firebug or similar?
xterm wroteKassem,

Did you try to track the requests (as a client) from firebug or similar?
Yup I did. And I've checked the documentation and apparently I'm going totally wrong about it. My main purpose is to simply invite them to the app which will simply redirect them to my registration page (once they accept the invitation) AND most importantly be able to track the person who invited them using an invitation code (which will basically be a Guid). In the docs, it says that the return data is actually the request_ids, which is not really what I'm looking for (although it could be useful to me).

EDIT: In firebug, I'm getting an object which contains the request_ids which in turn contains the ID's of the requests. But I did not see any trace of the value of the data property that I'm passing along with the dialog init() function.
you need to request the data of each request via the ID you get:
as per docs:
GET /ID/
then you need to DELETE /ID/
Ok that seems to be doing what I need to do but... Where should that PHP code be placed? Should that be placed inside the app itself? Because I was thinking I should be doing the tracking thing once someone accepts the invitation and lands on the application's page... Am I thinking all wrong about it?
Urgh! this is too confusing! One question that might clear things out... Should I think of the Facebook application as something completely separate from my website? I mean the code used to send the requests will be on the website, but the code that renders the interface of the application is supposed to be something completely separate from the site?
Hi guys,

Okay, first things first. Your app should not be separate from your website. For me, usually I use something like:
root
¦	files and folders
¦	
+---facebook
	¦
	¦
	+---app_name
		¦   index.php
		¦
		+---canvas
		¦   ¦   index.php
		¦   ¦
		¦   +---tab
		¦   ¦		index.php
		¦   ¦
		¦   +---css
		¦       	main.css
		¦       	reset.css
		¦
		+---src
				facebook.php
				fb_ca_chain_bundle.crt
(reference)
I put the Facebook app inside a folder called "facebook" because sometimes you may have more than one application on the same domain.

Also as you can see I have a folder called "tab" there I put all my "Facebook pages" related apps code, in the documentation:
Your Tab URL must be relative to your Canvas Page and therefore your Canvas URL. Using our previous Canvas Page and Canvas URL settings would results in Facebook loading content from "http://www.example.com/canvas/tab."
Okay, so now you have all your website under the "root" and all your Facebook related stuff under "facebook". As you can see it's all there under the same host (just like having a wordpress blog folder and a forum next to it).

A last note here, your "src" folder can be placed in the "facebook" folder if you are planning to have multiple apps

----------------------------

Now in your code above, users are sending invitations to their friends. These invitations have ids (obviously) which will allow you to access its details at a later stage.

Now the reason why storing these ids is important is described in my first tutorial:
The reason of doing this is to save the request ids along with the “sender/inviter” id ($uid). For me, this is very important since Facebook will only give you the ability to access this request by knowing the “receiver/invitee” or the request id!
So the only way to track these invitations is by knowing the "request id" (which why we are storing it) OR when the user's friend actually add the app where you can use this graph call "/me/apprequests" to get the requests ids!
Which (for me) is very stupid because for some reason if you lose the request id (or you didn't save it). And the user's friend never respond to the request you'll never know! and you'll never know if the user is actually sending anything to his friends!!

----------------------------
Kassem wroteYup I did. And I've checked the documentation and apparently I'm going totally wrong about it. My main purpose is to simply invite them to the app which will simply redirect them to my registration page (once they accept the invitation) AND most importantly be able to track the person who invited them using an invitation code (which will basically be a Guid). In the docs, it says that the return data is actually the request_ids, which is not really what I'm looking for (although it could be useful to me).
If the only thing you need to know when sending the users who are clicking on the invitation links is the "sender id" then no need for the "data" field at all because the "request" itself will have this piece of information. Refer to the documentation to check the request structure.

So the steps are:
1- the friend clicks on the link, gets redirected to your application INSIDE Facebook along with the request id/s he clicked
2- you read these "ids", extract the sender id, save it to the session or add it to your url (query string) and redirect the friend to your website!

----------------------------
Now some important notes to Kassem & ZeRaW:
ZeRaW wroteHey Kassem,
you can send data with the invitation (invitation code).
When the user clicks on accept it will redirect to the canvas URL of your app.
This is not necessary true!
From my second tutorial:
But be aware if you are setting the Bookmark URL in your application settings, Facebook will send the user to that url instead.
Also:
In your code, you are doing the following:
if($_REQUEST['request_ids']==$request_info['data'][$request_num]['id']) //If the 'request_ids' parameter is equal to the request data's sending ID.
This is not always true, based on the documentation:
If a user clicks 'Accept' on a request, they will be sent to the canvas URL of the application that sent the request with a new parameter ‘request_ids’, which is a comma delimited list of identifiers for the requests that a user trying to act upon; in this version, it will always be a single id since they are coming from the games dashboard, but it may be several in the future.
Also in your code, you are assuming that the landing use is already an app user since you are using "me"! which is again not always the case.

Also you are not deleting the request after processing it and you should remember:
As a best practice, you should upon landing, read the outstanding app requests for that user as well as the ids that the user is trying to act upon. Then, delete requests when the user ignores or handles them and potentially highlight the ones the user came in to act upon.

Requests 2.0 will not get deleted when a user clicks on accept in Facebook’s API. You must read and clear them using the Graph API.
Sorry for the long post :-)

ifaour
Xhack,

You have a beautiful and well organized website. Bravo.
xterm wroteXhack,

You have a beautiful and well organized website. Bravo.
Thanks man.
Ok I cannot thank you enough man! I believe it all makes sense now! :)

So, since all I need to do is redirect users to my registration page and track who actually invited them (in order to award the inviter), the index page of the FB app should do what you described above. But I still need the data property because I won't be relying on the FB ID of the sender (I have other methods of invitations as well, so I decided to add an InvitationCode to the Users table).

So basically I will be using the graph call "/me/apprequests" which will give me access to the whole request object, right? And from there I should be able to retrieve the value of the data property and append it to the redirect URL...

Did I finally get it? :)
@XhacK, this is not my code, it is from the comments on the Facebook Docs Page, as I said before I usually do not do anything special with the request id. I use it just as an invitation to the app without tracking who invited.
Kassem wroteSo, since all I need to do is redirect users to my registration page and track who actually invited them (in order to award the inviter), the index page of the FB app should do what you described above. But I still need the data property because I won't be relying on the FB ID of the sender (I have other methods of invitations as well, so I decided to add an InvitationCode to the Users table).
Fair enough, so the "data" field is needed in this case.
Kassem wroteSo basically I will be using the graph call "/me/apprequests" which will give me access to the whole request object, right? And from there I should be able to retrieve the value of the data property and append it to the redirect URL...

Did I finally get it? :)
Not really. :-)
the "me" object would refer to the current "connected" user, which in your case will be NULL!

Friends clicking on the invitation links will (most likely) not be an application users (yet). So when they land on the canvas page or the bookmark page, "me" will be null!

As I said in the previous post and my article, Facebook will send the request ids in the query string (you should use GET to receive those) and once you have these requests you read them (check my second tutorial for this), extract the "data" field (or whatever info you want) and send them to your site.
We're talking about a different thing I guess... I want to be able to track the code in the data field because I want to award the invited when that invited user registers on my site.

I am assuming that when a user accepts the invitation to the facebook app it will take them to the app page itself, right? And that is where I should extract the code from the data field and then all what the application page will do is to simply redirect the user to mysite.com/User/Register?code=<the extracted code>