Note: You can download the latest ASPSnippets.FaceBookAPI.dll clicking the download link below.
Create FaceBook Application and get App Id
You will need to create an application and get an API Key and API Secret
HTML Markup
In the below HTML Markup, I have an ASP.Net Button which triggers the FaceBook Album Images fetching process. Once the Profile Picture or Image is available it is displayed in the Image Control.
<asp:Button ID="btnFetch" runat="server" Text="Get FaceBook Pictures" OnClick="Fetch" />
<hr />
<asp:Image ID="imgProfile" runat="server" Visible="false" />
Namespaces
You will need to import the following Namespaces.
C#
using ASPSnippets.FaceBookAPI;
using System.Web.Script.Serialization;
using System.Collections.Generic;
VB.Net
Imports ASPSnippets.FaceBookAPI
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Data Class
You will need to create the following classes which will be used to hold the FaceBook Albums and the respective Album images returned from FaceBook after authentication.
C#
public class Album
{
public FaceBookAlbums Albums { get; set; }
public String Id { get; set; }
}
public class FaceBookAlbums
{
public List<FaceBookAlbum> Data { get; set; }
}
public class FaceBookAlbum
{
public String Id { get; set; }
public String Name { get; set; }
public List<FaceBookImage> Images { get; set; }
}
public class FaceBookImageData
{
public List<FaceBookImage> Data { get; set; }
}
public class FaceBookImage
{
public String Source { get; set; }
}
VB.Net
Public Class Album
Public Property Albums() As FaceBookAlbums
Public Property Id() As String
End Class
Public Class FaceBookAlbums
Public Property Data() As List(Of FaceBookAlbum)
End Class
Public Class FaceBookAlbum
Public Property Id() As String
Public Property Name() As String
Public Property Images() As List(Of FaceBookImage)
End Class
Public Class FaceBookImageData
Public Property Data() As List(Of FaceBookImage)
End Class
Public Class FaceBookImage
Public Property Source() As String
End Class
Fetching and importing the User Album Pictures from FaceBook
On the click of the button, first the application authorizes the user and asks for permission to access its Albums and pictures.
Once the user has granted the permission, all the Albums are fetched and then the Id of the Album named Profile Pictures is determined as it contains the profile images of the FaceBook user. Once the Id is available then the images for the album are fetched and then the very first image of the Album and displayed in the image control.
C#
protected void Page_Load(object sender, EventArgs e)
{
FaceBookConnect.API_Key = "<Your API Key>";
FaceBookConnect.API_Secret = "<Your API Secret>";
if (!IsPostBack)
{
String code = Request.QueryString["code"];
if (!String.IsNullOrEmpty(code))
{
//Fetch User Albums.
String data = FaceBookConnect.Fetch(code, "me/?fields=albums");
Album faceBookAlbum = new JavaScriptSerializer().Deserialize<Album>(data);
FaceBookAlbum album = faceBookAlbum.Albums.Data.Find(a => a.Name == "Profile Pictures");
//Fetch Images for the "Profile Pictures" Album.
data = FaceBookConnect.Fetch(code, album.Id + "/photos?fields=source");
album.Images = new JavaScriptSerializer().Deserialize<FaceBookImageData>(data).Data;
//The First Image is the Profile Picture of the user.
imgProfile.ImageUrl = album.Images[0].Source;
imgProfile.Visible = true;
}
}
}
protected void Fetch(object sender, EventArgs e)
{
FaceBookConnect.Authorize("user_photos", Request.Url.AbsoluteUri.Split('?')[0]);
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
FaceBookConnect.API_Key = "<Your API Key>"
FaceBookConnect.API_Secret = "<Your API Secret>"
If Not IsPostBack Then
Dim code As String = Request.QueryString("code")
If Not String.IsNullOrEmpty(code) Then
'Fetch User Albums.
Dim data As String = FaceBookConnect.Fetch(code, "me/?fields=albums")
Dim faceBookAlbum As Album = New JavaScriptSerializer().Deserialize(Of Album)(data)
Dim album As FaceBookAlbum = faceBookAlbum.Albums.Data.Find(Function(a) a.Name = "Profile Pictures")
'Fetch Images for the "Profile Pictures" Album.
data = FaceBookConnect.Fetch(code, album.Id + "/photos?fields=source")
album.Images = New JavaScriptSerializer().Deserialize(Of FaceBookImageData)(data).Data
'The First Image is the Profile Picture of the user.
imgProfile.ImageUrl = album.Images(0).Source
imgProfile.Visible = True
End If
End If
End Sub
Protected Sub Fetch(sender As Object, e As EventArgs)
FaceBookConnect.Authorize("user_photos", Request.Url.AbsoluteUri.Split("?")(0))
End Sub
Screenshot
Demo
Downloads