-
-
Save jeffgabhart/6978509 to your computer and use it in GitHub Desktop.
Get Facebook Friends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Friend | |
{ | |
public String Id { get; set; } | |
public String Name { get; set; } | |
}; | |
private class Paging | |
{ | |
public String next { get; set; } | |
public String previous { get; set; } | |
}; | |
private class Friends | |
{ | |
public List<Friend> data { get; set; } | |
public Paging paging { get; set; } | |
}; | |
static private string DownloadFacebookFriends(string AccessTokenSecret, string FacebookFriendsUrl = "https://graph.facebook.com/me/friends?access_token={0}") | |
{ | |
var url = FacebookFriendsUrl.Fmt(AccessTokenSecret); | |
var json = url.DownloadUrl(); | |
return json; | |
} | |
static private List<Friend> GetFacebookFriends(string AccessTokenSecret) | |
{ | |
List<Friend> friends; | |
friends = new List<Friend>(); | |
string friendsJson = DownloadFacebookFriends(AccessTokenSecret); | |
bool hasNext = true; | |
while (hasNext) | |
{ | |
Friends pagingFriends = JsonSerializer.DeserializeFromString<Friends>(friendsJson); | |
friends.Concat(pagingFriends.data); | |
if (pagingFriends.paging.next == null) | |
{ | |
hasNext = false; | |
} | |
else | |
{ | |
friendsJson = DownloadFacebookFriends(AccessTokenSecret, pagingFriends.paging.next); | |
} | |
} | |
return friends; | |
} | |
static public List<Friend> GetFacebookFriendsFromSession(AuthUserSession session) | |
{ | |
List<Friend> friends; | |
var tokens = AuthSessionExtensions.GetOAuthTokens(session, "Facebook"); | |
var AccessTokenSecret = tokens.AccessTokenSecret; | |
friends = GetFacebookFriends(AccessTokenSecret); | |
return friends; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment