jvcoach23
jvcoach23

Reputation: 3037

Facebook posting to a page not the user wall

Need help posting to a page in facebook. I used this article Post on Facebook User's wall and it works. However, the place where i am trying to post is not the persons wall, but instead, when the user is logged into face book and you click on the gear symbol on the upper right, it says "Use Facebook as: ThingX" and then they select ThingX. That takes them to ThingX page and that is where i want to post to. In code if i log in as the person, it post to their wall and not ThingX. How do i post to ThingX wall.

i'm passing in publish_stream,manage_pages as the code.. i didn't know if i needed to do something different there..

Here is the code that i'm using now.. vb.net

Dim app_id As String = "xxxxxxxxxxxx"
    Dim app_secret As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    Dim scope As String = "publish_stream,manage_pages"

    If Request("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))
    Else
        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, Request("code").ToString(), app_secret)

        Dim request__1 As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)

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

            Dim vals As String = reader.ReadToEnd()

            For Each token As String In vals.Split("&"c)
                'meh.aspx?token1=steve&token2=jake&...
                tokens.Add(token.Substring(0, token.IndexOf("=")), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1))
            Next
        End Using

        Dim access_token As String = tokens("access_token")

        Dim client = New FacebookClient(access_token)

        client.Post("/me/feed", New With { _
         Key .message = "This is a test message -- " & Now.ToShortTimeString _
        })
    End If

wondered if anyone could lend a hand. thanks shannon

~~~~~~~~~~~~~~~~~~~~~~~~~ Got it working.. code below Well.. i finally got this working, where i could post to a users page. To give credit where credit is due.. http://www.markhagan.me/Samples/Grant-Access-And-Post-As-Facebook-User-ASPNet got me part of the way.. and then the author of that link was kind enough to help me out to get the rest of the way. I used Newtonsoft to help me with some JSon work. Here is the code.. hope it can help someone else out.. and thanks again Mark.

the offline_access lets you create a token that will not expire.

Dim app_id As String = "your app id"
    Dim app_secret As String = "your app secret"
    Dim scope As String = "publish_stream,manage_pages,offline_access"




    If Request("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))
    Else
        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, Request("code").ToString(), app_secret)
        Dim request__1 As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)

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

            Dim vals As String = reader.ReadToEnd()

            For Each token As String In vals.Split("&"c)
                        tokens.Add(token.Substring(0, token.IndexOf("=")), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1))
            Next
        End Using

    Try
        Dim access_token As String = tokens("access_token")

        Dim client = New FacebookClient(access_token)

        ' <-- put your USER access token here
        Dim p As JsonObject = CType(client.[Get]("/me/accounts"), JsonObject)

        Dim acc As jsondata = Newtonsoft.Json.JsonConvert.DeserializeObject(Of jsondata)(p.ToString)
        Dim accData As New List(Of AccountData)
        accData = acc.data


        Dim mList = From w In accData _
                  Where w.ID = CStr("your id value that came back through JSON) _
                  Select w

        Dim selected As New AccountData

        For Each selected In mList.ToList
        Next

        Dim postparameters = New Dictionary(Of String, Object)()
        postparameters("message") = Me.txtText.Text

        Dim client1 = New FacebookClient(selected.access_token)
        Dim result = DirectCast(client1.Post("/me/feed", postparameters), IDictionary(Of String, Object))

        Dim postid = DirectCast(result("id"), String)
        Return String.Empty
    Catch ex As Exception
        Return ex.Message.ToString
    End Try
    End If

you'll also need these two classes

Private Class AccountData
    Public Property Name As String
    Public Property Category As String
    Public Property ID As String
    Public Property access_token As String
End Class
Private Class jsondata
    Public Property data As New List(Of AccountData)
End Class

Upvotes: 1

Views: 895

Answers (1)

Michael
Michael

Reputation: 715

Well... you're posting to "/me/feed", which'll post to the person's feed. To post to another user's or a page's wall, you need to be posting to the pages id.

https://developers.facebook.com/docs/reference/api/publishing/

Quote:

You can publish to quite a few different kinds of objects via the Graph API:

Method  Description Arguments
/PROFILE_ID/feed    Publish a new post on the given profile's timeline. Note: requires publish permissions for the targeted profile.    message, picture, link, name, caption, description, source, place, tags
/OBJECT_ID/comments Comment on the given object (if it has a /comments connection)
and so on..

Upvotes: 1

Related Questions