VB.NET Facebook C# SDK to post on users profile and pages


VB.NET Facebook C# SDK to post on users profile and pages

Throughout digging in to internet finally i am writing this for VB.net geeks:

Step1 : Install FB C# SDK through Nuget in Visual studio 2010. (Install Nuget first if dont have)

Step2: Creat new VB.new Windows Project.

Step3. Right click project >  Manage Nuget Packages (Its below add reference)

Now you are ready to have reference to use FB C# SDK in Vb.Net App without creating/converting DLL.

Open your Form.vb
and import Facebook.

Here is an simple example to get detail of any fb userid.
Prerequisite.
> Create a simple test app in FB developer 
> Go to your apps permission and give some extended permission like: publish_stream,offline_access,read_stream,manage_pages

> Create a webbrowser control and textbox in your form
> nevigate using webbrowser useyour app id as client id
> on run webbrowser  will ask your user id and password and allow permission for app.. at the end it will leave u with current access token which u can use to post on page as well as profile since we have given the permission.
 

Imports Facebook
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 WebBrowser1.Navigate("https://www.facebook.com/dialog/oauth?client_id=293068924149***&redirect_uri=https://www.facebook.com/connect/login_success.html &scope=publish_stream,offline_access,read_stream,manage_pages&response_type=token")
       

    End Sub

    Private Sub WebBrowser1_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
    If WebBrowser1.Url.AbsoluteUri.ToString.Contains("access_token") Then
            Dim url = WebBrowser1.Url.AbsoluteUri.ToString
            Dim str1 = url.Substring(url.IndexOf("access_token") + 13)
            Dim accesstoken = str1.Substring(0, str1.IndexOf("&"))
            Dim fb1 = New FacebookClient(accesstoken)
            Dim result1 = fb1.[Get]("/vinodkotiya/") '"vinodkotiya?fields=id"
            TextBox1.Text = result1.ToString
            Dim args = New Dictionary(Of String, Object)()
            args("message") = "Test post from .net app BotPost to my fb Profile wall- Life Runs on code...now-" & Now.ToString
             result1 = fb1.Post("/320142621436288/feed", args) 'fb page id
            result1 = fb1.Post("/vinodkotiya/feed", args) 'fb wall
            TextBox1.Text = result1.ToString
        End If

    End Sub
End Class



So… why? According to the documentation, manage_pages is the only permission required for an application to access a page, and everything seemed in order. Actually, it wasn’t. After some tests going back and forth and some googleing, I realized I didn’t have enough scope, and adding publish_stream and read_streamseemed to be a requirement for the application to actually write to page’s wall. In addition, offline_accesswould produce a token that does not expire.

- Written by
Vinod Kotiya

Comments

Popular Posts