Facebook API


SUBMITTED BY: Guest

DATE: Aug. 1, 2014, 10:14 p.m.

FORMAT: C#

SIZE: 5.0 kB

HITS: 25001

  1. Imports System.Net
  2. Imports System.IO
  3. Imports System.Threading
  4. Imports System.Web.Script.Serialization 'Reference needed!
  5. Class FacebookAPI
  6. Private URL As String = "http://graph.facebook.com/"
  7. Public Event Completed(ByVal sender As Object, ByVal e As FacebookEventArgs)
  8. Public Event [Error](ByVal sender As Object, ByVal ex As Exception)
  9. Public Sub FetchData(ByVal ID As String)
  10. Try
  11. Using WC As New WebClient
  12. WC.DownloadStringAsync(New Uri(URL & ID & "?fields=id,username,name,first_name,last_name,gender,picture,cover"))
  13. AddHandler WC.DownloadStringCompleted, AddressOf DeserializeData
  14. End Using
  15. Catch ex As Exception
  16. RaiseEvent Error(Me, ex)
  17. End Try
  18. End Sub
  19. Private Sub DeserializeData(sender As Object, e As DownloadStringCompletedEventArgs)
  20. Try
  21. If e.Cancelled Then Return
  22. If e.Error IsNot Nothing Then Return
  23. Dim t As New Thread(Sub()
  24. Dim args As New FacebookEventArgs
  25. For Each node As KeyValuePair(Of String, Object) In New JavaScriptSerializer().Deserialize(Of Dictionary(Of String, Object))(e.Result)
  26. Dim str As String = node.Value.ToString
  27. Select Case node.Key
  28. Case "id" : args.ID = str
  29. Case "username" : args.Username = str
  30. Case "name" : args.Name = str
  31. Case "first_name" : args.FirstName = str
  32. Case "last_name" : args.LastName = str
  33. Case "gender" : args.Gender = str
  34. Case "picture"
  35. Dim picJSON As Object = Nothing
  36. Dim picURL As Object = Nothing
  37. If DirectCast(node.Value, Dictionary(Of String, Object)).TryGetValue("data", picJSON) Then
  38. If DirectCast(picJSON, Dictionary(Of String, Object)).TryGetValue("url", picURL) Then
  39. args.PictureURL = picURL.ToString
  40. args.Picture = ConvertImage(picURL.ToString)
  41. End If
  42. End If
  43. Case "cover"
  44. Dim coverDict As Dictionary(Of String, Object) = DirectCast(node.Value, Dictionary(Of String, Object))
  45. If coverDict.ContainsKey("offset_y") Then args.CoverOffset = Integer.Parse(coverDict("offset_y").ToString)
  46. If coverDict.ContainsKey("source") Then
  47. args.CoverURL = coverDict("source").ToString
  48. args.Cover = ConvertImage("https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/" & args.CoverURL.Substring(55, args.CoverURL.Length - 55).ToString)
  49. End If
  50. End Select
  51. Next
  52. RaiseEvent Completed(Me, args)
  53. End Sub)
  54. t.Start()
  55. Catch ex As Exception
  56. RaiseEvent Error(Me, ex)
  57. End Try
  58. End Sub
  59. Private Function ConvertImage(ByVal URL As String) As Image
  60. Try
  61. Using WC As New WebClient()
  62. Using MS As New MemoryStream(WC.DownloadData(URL))
  63. Using B As New Bitmap(MS)
  64. Return B
  65. End Using
  66. End Using
  67. End Using
  68. Catch ex As Exception
  69. RaiseEvent Error(Me, ex)
  70. Return Nothing
  71. End Try
  72. End Function
  73. Public Class FacebookEventArgs
  74. Inherits EventArgs
  75. Public ID As String
  76. Public Username As String
  77. Public Name As String
  78. Public FirstName As String
  79. Public LastName As String
  80. Public Gender As String
  81. Public PictureURL As String
  82. Public CoverURL As String
  83. Public CoverOffset As Integer
  84. Public Picture As Image
  85. Public Cover As Image
  86. End Class
  87. End Class

comments powered by Disqus