Imports System.Text Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Json ' Tutorial Teil 3: Facebook mit Visual Basic ' http://www.Frank-IT-Beratung.com/blog/ Public Class Form1 Dim myFriendList As New FriendList Private Sub cmdSenden_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSenden.Click Dim Nachricht, Antwort As Byte() Dim AccessToken As String AccessToken = txtAccessToken.Text If AccessToken = "" Then MsgBox("Bitte erst auf 'Login' klicken") Return End If 'postet die Nachricht auf der Wall des Users 'der das AccessToken generiert hat: Dim GraphURL As String = "https://graph.facebook.com/me/feed" If comboFreunde.Text <> "me" Then GraphURL = "https://graph.facebook.com/" & _ myFriendList.Friend(comboFreunde.SelectedIndex - 1).id & _ "/feed" End If 'MsgBox(GraphURL) 'WebClient anlegen Dim myWebClient As New Net.WebClient 'Except100 ausschalten, sonst gibt es u. U. Fehlermeldungen Net.ServicePointManager.Expect100Continue = False Nachricht = Encoding.UTF8.GetBytes("message=" & _ txtStatusupdate.Text & _ "&access_token=" _ & AccessToken) Try Antwort = myWebClient.UploadData(GraphURL, Nachricht) 'Erfolgsmeldung (ID des Posts) in JSON 'MsgBox(System.Text.Encoding.ASCII.GetString(Antwort)) MsgBox("Erfogreich gepostet!") Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click Dim u As String = "https://graph.facebook.com/oauth/authorize?" 'Anwendungs-ID - hier statt ??? Ihre App ID eintragen u = u + "client_id=???" 'Berechtigung u = u + "&scope=publish_stream" 'URL, die nach Aufruf angezeigt wird u = u + "&redirect_uri=http://www.facebook.com/connect/login_success.html" 'Login-Typ u = u + "&type=user_agent" u = u + "&display=popup" WebBrowser1.Navigate(u) End Sub Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated 'der Access-Token hängt an der URL Dim q As String = WebBrowser1.Url.ToString() 'jetzt auslesen If q.Contains("#access_token=") Then q = q.Substring(q.IndexOf("access_token=") + 13) txtAccessToken.Text = q.Substring(0, q.IndexOf("&")) End If End Sub Private Sub cmdFreundeLaden_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFreundeLaden.Click Dim Antwort As Byte() Dim myWebClient As New Net.WebClient Try Antwort = myWebClient.DownloadData("https://graph.facebook.com/me/friends?access_token=" _ & txtAccessToken.Text) 'MsgBox(System.Text.Encoding.ASCII.GetString(Antwort)) MsgBox("Freundeliste geladen!") Catch ex As Exception MsgBox(ex.Message) Return End Try 'JSON-Antwort deserialisieren Dim ser As New DataContractJsonSerializer(GetType(FriendList)) Dim ms As New MemoryStream(Antwort) myFriendList = TryCast(ser.ReadObject(ms), FriendList) 'Zum Test ersten Freund ausgeben: MsgBox("Name: " & myFriendList.Friend(0).name & _ " ID: " & myFriendList.Friend(0).id) 'Mich und alle Freunde in die ComboBox: comboFreunde.Items.Add("me") For i = 0 To myFriendList.Friend.Length - 1 comboFreunde.Items.Add(myFriendList.Friend(i).name & " (ID: " & myFriendList.Friend(i).id & ")") Next i comboFreunde.SelectedIndex = 0 End Sub End Class