Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get conversations of FB page via open graph api

I've registered an Facebook-App and created a token with all permissions I need. Among them

  • manage_pages
  • read_mailbox
  • read_page_mailbox

Now I want to use the Facebook open graph API to read some data from my own FB-page. I want to read the private messages that the page received and that I sent to some of the fans on my pages behalf.

I know I can get all information by sending a http-request like this:

https://graph.facebook.com/{page-id}/{object}?access_token={token}

where {something} are placeholders for actual values. To give an working example, I can read the postings that appeared on my page by sending this request:

https://graph.facebook.com/141928949155955/posts?access_token={my secret token}

As I said, this works fine, since more than two years. (I just need to update the token from time to time)


But now I want to read the private conversations between the page and it's fans. I want to extract all conversations and insert them into a spreadsheet. I want to do it once, and maybe in 1 or 2 years again. I could extract them manually from the browser window by copy and paste for each conversation, but since there are so many conversations I think it costs less time to let a program do it for me.

If I understand Facebooks Documentation correct, then the keyword that I must use must be conversations. But I get this:

https://graph.facebook.com/141928949155955/conversations?access_token={secret token}  

{
   "error": {
      "message": "(#210) Subject must be a page.",
      "type": "OAuthException",
      "code": 210
   }
}

But 141928949155955 is a page. I don't know what I'm doing wrong. What is the correct request that I must send to receive a pages private conversations?


EDIT (June 16th):

I can read my personal conversations with this request:

https://graph.facebook.com/me/conversations?access_token={secret token}  

But I don't want to read the conversations that I made as a person. I need those of one of my pages.

like image 950
Hubert Schölnast Avatar asked Jun 14 '14 09:06

Hubert Schölnast


People also ask

How do I get data from Facebook Graph API?

Open the Graph Explorer in a new browser window. This allows you to execute the examples as you read this tutorial. The explorer loads with a default query with the GET method, the lastest version of the Graph API, the /me node and the id and name fields in the Query String Field, and your Facebook App.

How do I get Facebook Page Insights on graph API?

You can access Page Insights by typing /insights after the name of your page in the URL field. This command will retrieve all of the Insights associated with your Page. Type "/insights" after your company name. Click the Submit button.

What is Facebook conversation API?

A Messenger conversation between a person and a Facebook Page or an Instagram Professional Account.

Is there an API for Facebook Messenger?

The Messenger Profile API allows you to set, update, retrieve, and delete properties from the Page Messenger Profile.


2 Answers

I found the solution for my problem. (Thanks kush, your answer helped a lot, but I want to add some more information)

My problem was that I misunderstood the error message "Subject must be a page." I thought that "Subject" refers to a part in the query string. But it refers to the token! A message like "Token must be a Page Access Token" would have saved me WEEKS of needless searching over a period of more than two years! How can you use a term like "Subject" when you nowhere define what "Subject" is?

So it was not the query string that was wrong. It was the type of the token!

Now here is the complete procedure to solve the problem (get a Page Access Token)


SOLUTION

Step 1

Get a User Access Token for your App that you can use later to get a Page Access Token

  • 1.1 Go to Graph API Explorer https://developers.facebook.com/tools/explorer/
  • 1.2 Select your App from the "Application:" drop down menu.
  • 1.3 Click "Get Access Token".
  • 1.4 From the tab "Extended Permissions" select "manage_pages".
    • Additionally select all permissions you need for the Page Access Token. (Although now you don't create a Page Access Token, just a User Access Token)
  • 1.5 Click "Get Access Token".
  • 1.6 Grant the permissions by clicking "OK".

In the field "Access Token:" you find now a Token that is an User Access Token. It is not a Page Access Token! This Token has all permissions you need to manage your page (including the permission "manage_pages"), but you can't use it to manage pages since it is the wrong type of token. You need this token only for one reason: To create a new token.

Step 2

Use the User Access Token you just got to create a Page Access Token

  • 2.1 Enter "me/accounts" in the query field
  • 2.2 Click "Submit"

You get a list of your pages, each with an "access_token" (which is now a Page Access Token). Each of this tokens did inherit the permissions from the User Access Token you used when you called "me/accounts".

This Page Access Token expires after one hour.


supplement

If you want a Page Access Token that never expires, do it this way:

  • A.1 Go to https://developers.facebook.com
  • A.2 From the drop down menu "Apps" select the App you want to use.
  • A.3 Click on "show" in the field "App Secret" (you need to enter your Facebook Password)
  • A.4 Open a new tab in your browser and there execute steps 1.1 to 1.6 from above to generate a short-lived User Access Token
  • A.5 Open a third Tab in your browser and there enter this string into the address field of your browser:
    • https://graph.facebook.com/v2.0/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={short-lived-token} (do not press enter!) If this strings disappears when switching from one browser tab to another use any text editor to assemble the string and copy it later into the address field.
  • A.6 In this string replace {app-id} and {app-secret} by the values from the browser tab that still shows your Apps data.
  • A.7 Replace {short-lived-token} with the User Access Token you find in the field "Access Token:" from the Graph API Explorer.
    • (After this step there are no more curly brackets in the address string)
  • A.8 send this request (press Enter now)

Your browser window now shows a String with the fields "access_token" and "expires", separated by an ampersand ("&") which is not part of the token. This Token is a long lived User Access Token that will expire in two month.

  • A.9 Execute Steps 2.1 and 2.2 using the "access_token" from step A.8

The result is a Page Access Tokes with all permissions you selected in Step 1.4 and that never will expire.

Good luck!

like image 135
Hubert Schölnast Avatar answered Nov 15 '22 09:11

Hubert Schölnast


Using page access token should help since you are reading from the page ..

Graph api node: GET me/accounts

me/accounts would give you the pages you are admin for and use access token coming in the page info dictionary.

you should get the following data from me/accounts

{
  "data": [
    {
      "category": "Product/service",
      "name": "Sample Page",
      "access_token": "{page-access-token}",
      "id": "1234567890",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    }, 
}

Use page-access-token to access data from page as the page admin and thou shall receive data :) let me know if this works :)

like image 38
kush Avatar answered Nov 15 '22 09:11

kush