Wednesday, November 14, 2012

Creating a facebook app

This is a step by steb tutorial for creating a facebook app. At first you have to register as a facebook developer in  https://developers.facebook.com/ , Then download the dll's FBHelper.dll and NewtoSoft.dll from this link

step:1 Navigate to https://developers.facebook.com/ and click on the app link.

 

step:2 click on the create new app button


step:3 Enter the app name and name space. name space is simple a name which is used to identify   your app.


step 4: Complete the capche.

step:5  You will get a unique app id and app secret code. Sandboxmode should be enabled while creating and testing your app.


step 6: Open your visual studio and create an asp.net website. this is for getting your testing url.

step 7: add new page.
 

step 8: add the following dll's to your projects. Newtonsoft is not requered unless you are not using the direct FQL method from the FBHelper.

 
 step 9: Take the properties of your website by rightclicking on the site annd change the target framework to 4, because FBHelper.dll supports only from dotnet 4.0.

step 10: take your site url by running your website.


step 11: https://developers.facebook.com/ and change your canvase url. this url will be called after the user authenticate your app.

step 12:  Customize your permissions.

step 13: add
using FBHelper;
using FBHelper.Entity;  namespaces. then you are ready to go. see the below screen shots


Thursday, November 8, 2012

Facebook graph api

Sample application created using the following dll's

Get your blessing word https://christopher.azurewebsites.net/ 

You can create a facebook application simply using these dll's.
download these dll's from the following links.
FBHelper.dll
NewtonSoft JSON

then you need to create a facebook app using the link fbapp and then navigate to create facebook app page and do the processings and the get the appID and appsecret.

then


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using FBHelper;
using FBHelper.Entity;
using System.IO;


namespace FacebookApp
{
    public partial class AppPage : System.Web.UI.Page
    {
        FB fbApp = new FB();

        protected void Page_Load(object sender, EventArgs e)
        {
            string imageUrl = Server.MapPath(@"/image/Hydrangeas.jpg");

            fbApp.AppID = "YOUR_APP_ID";
            fbApp.AppSecret = "YOUR_APP_SECRET";
            fbApp.RedirectURL = "http://localhost:51337/AppPage.aspx";
            string code = Request.QueryString["code"] as string;
            string error = Request.QueryString["error_description"] as string;

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(code))
                {
                    Response.Redirect(fbApp.CodeRequestURL);
                }
                else
                {
                    fbApp.Code = code;
                    fbApp.GetAccessToken();
                    string token = fbApp.AccessToken;
                    FBUser user = fbApp.GetMe();
                    List<FBUser> friends = fbApp.GetMyFriends();
                    List<Photo> pics = fbApp.GetMyPhotos();

                    string statusID = fbApp.UpdateStatus(user.ID, "hiiiii");
                    string album = fbApp.CreateAlbum(user.ID, "Sample", "new sample");
                    List<Album> alb = fbApp.GetAllAlbumDetails();
                    List<Likes> like = fbApp.GetAllLikesByMe();

                    fbApp.UploadPhoto("Sample Image", imageUrl);
                    fbApp.UploadPhotoToAnAlbum(album, "Hiiiiii", imageUrl);
                }
            }
        }
    }
}




Facebook direct fql query using c#





string query= "select aid,name,cover_pid,created,modified,description from album where owner=me()";
string data = string.Empty;
Uri uri = new Uri("https://graph.facebook.com fql?q=" + query + "&access_token=" + accessToken);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = (Stream)response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
data = reader.ReadToEnd();

Facebook get all album



first you should check the permisson on facebook https://developers.facebook.com/docs/reference/login/user-friend-permissions/

string data = string.Empty;
Uri uri = new Uri("https://graph.facebook.com/me/albums?access_token=" + token);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = (Stream)response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
data = reader.ReadToEnd();

Facebook get all photos

first you should check the permisson on facebook https://developers.facebook.com/docs/reference/login/user-friend-permissions/



string data = string.Empty;
Uri uri = new Uri("https://graph.facebook.com/me/photos?access_token=" + token);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = (Stream)response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
data = reader.ReadToEnd();

Facebook get all friends



frist you should check the permisson on facebook https://developers.facebook.com/docs/reference/login/user-friend-permissions/

string data = string.Empty;
Uri uri = new Uri("https://graph.facebook.com/ me/friends?access_token=" + token);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = (Stream)response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
data = reader.ReadToEnd();

Facebook Get loggedin user details using c#



 
string data = string.Empty;
Uri uri = new Uri("https://graph.facebook.com/me?access_token=" + token);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = (Stream)response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
data = reader.ReadToEnd();

Facebook Upload Photo using c#

You can either create one album and upload to the album, otherwise you can upload to the default album of your app.
In order to publish a photo to a user’s album, you must have the publish_stream permission. With that granted, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections:
  • https://graph.facebook.com/USER_ID/photos - The photo will be published to an album created for your app. We automatically create an album for your app if it does not already exist. All photos uploaded this way will then be added to this same album.
  • https://graph.facebook.com/ALBUM_ID/photos - The photo will be published to a specific, existing photo album, represented by the ALBUM_ID. 
 Upload to the default album



WebClient client = new WebClient();
client.UploadFile("https://graph.facebook.com/me/photos?access_token=" + accessToken + "&message=" + name, "POST", path);  


upload to the particular album


WebClient client = new WebClient();
client.UploadFile("https://graph.facebook.com/" + albumID + "/photos?access_token=" + accessToken + "&message=" + name, "POST", path);