ASP.Net VB Facebook SDK Post on User Wall or Any Page



Dim app_id As String = "654674765764"
            Dim app_secret As String = "dgfdgy465yfhy675"
            Dim scope As String = "publish_actions, manage_pages, publish_pages, public_profile"

        If Session("code") Is Nothing Then
            Response.Redirect(String.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}", app_id, Request.Url.AbsoluteUri, scope))
            Exit Sub
        End If
        Dim tokens As New Dictionary(Of String, String)()


        Dim url As String = String.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}", app_id, Request.Url.AbsoluteUri, scope, Session("code").ToString(), app_secret)
        'Dim url = "https://graph.facebook.com/oauth/access_token?client_id=" & app_id & "&redirect_uri=" & Request.Url.AbsoluteUri.ToString & "&scope=" & scope & "&code={3}&client_secret=" & app_secret
        Dim request__1 As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)

            Using response As HttpWebResponse = TryCast(request__1.GetResponse(), HttpWebResponse)
                Dim reader As New StreamReader(response.GetResponseStream())

                Dim vals As String = reader.ReadToEnd()
                'Dim vals As String = "{'access_token':'our token value here','token_type':'bearer'}"
                Dim obj = JObject.Parse(vals)
            Dim access_token As String = DirectCast(obj("access_token").ToString, String)
            '' Now you have got user access token if you do anything onwards then it will be done as a user. To do like a page get  Page Access Token
            '            The last important Access Token Is used To manage Facebook Pages. It´s a bit more complicated To Get that Token, these are the required steps:
            'after getting a valid User Access Token with the additional permission “manage_pages”
            'make a call to /PAGE-ID?fields=access_token with the Facebook API
            'get Page Access Tokens for all your Facebook Pages in an Array


            Dim fbclient = New FacebookClient(access_token)
            Dim fbpage = "/135771030321215/feed"
            Dim fbpagefoto = "/135771030321215/photos"
            Dim msg = "Going Hi Tech. Test Post. From Monday this page will be run by BOTS. No Human intervention." & Now.ToString()
            Dim pic = Server.MapPath("./images/botpic/") & "muktinath.jpg"
            ''dim fbprofile = "/vinodkotiya/feed"
            fbclient.Post(fbpage, New With {
                             Key .message = msg
                                })

            'Dim imgstream = File.OpenRead(pic)

            'Dim res = fbclient.Post(fbpagefoto, New With {
            ' .message = msg,
            ' .file = New FacebookMediaStream() With {
            '  .ContentType = "image/jpg",
            '  .FileName = Path.GetFileName(pic)
            ' }.SetValue(imgstream)
            '})
            divMsg.InnerHtml = access_token
            End Using


Comments