VB.NET Facebook C# SDK to post pics on fan 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  to post pics on fan pages
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 use your 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.

Now we will implement following model in our code:

1. webbrowser will access the app id through url and return accesstoken
2. again use /userid/accounts to get all pages admin by you with accesstoken save desired one in your pageaccesstoken variable  ... to see how it works do this 

https://graph.facebook.com/youraccount/accounts?access_token=
https://graph.facebook.com/me/accounts?access_token=

3. now use page access token to do whatever you want to do in your page..

Code

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
 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/accounts") '"vinodkotiya?fields=id"
            '  TextBox1.Text = result1.ToString & " account access token " & accesstoken   'return all my pages access token
            url = result1(0).ToString 'assumin 1st record is my desired page 
            str1 = url.Substring(url.IndexOf("access_token") + 15)
            Dim pageaccesstoken = str1.Substring(0, str1.IndexOf(",") - 1)
            TextBox1.Text = pageaccesstoken & " account access token " & accesstoken
            ' Exit Sub
            Dim suc = "Eureka.. finally found code to get access token to post pics on my fan page ..."
            Dim fb2 = New FacebookClient(pageaccesstoken)
            Dim args = New Dictionary(Of String, Object)()
            args("message") = suc & "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
            ' result1 = fb1.Post("/vinodkotiya/feed", args) 'fb wall
            ' Exit Sub
            Dim imgstream = File.OpenRead("d:\fb.jpg")

            Dim res = fb2.Post("/320142621436288/photos", New With { _
             .message = suc & "Life runs on code : Tst pic from BotPost app to fan page now-" & Now.ToString, _
             .file = New FacebookMediaStream() With { _
              .ContentType = "image/jpg", _
              .FileName = Path.GetFileName("d:\fb.jpg") _
             }.SetValue(imgstream) _
            })
            TextBox1.Text = res.ToString
        End If

    End Sub
End Class


- Written By
Vinod Kotiya
www.vinodkotiya.com

Comments

Miguel Angel said…
Hi, I have problems to use this code. I create all, and I replace your appid (293068924149***) to mine. If I put the url (https://www.facebook.com/dialog/oauth?client_id=xxxxxxxxxxxx&redirect_uri=https://www.facebook.com/connect/login_success.html &scope=read_stream,manage_pages&response_type=token) in the internet explorer it say me Success, but when I execute the program this line return the same url

Dim str1 = url.Substring(url.IndexOf("access_token") + 13)

return to me

"facebook.com/dialog/oauth?client_id=xxxxxxx&redirect_uri=https://www.facebook.com/connect/login_success.html%20&scope=read_stream,manage_pages&response_type=token"

and finally it crash in Dim result1 = fb1.[Get]("/miguelangel.alegriaecheverria/accounts")

I don't know where I wrong