getting friendlist from facebook graph-api - python

I am trying to get user's friend list from facebook Graph-api. So after getting access token when I try to open by urlopen by
https://graph.facebook.com/facebook_id/friends?access_token=authentic_accesstoken
this doesn't give friend_list of person(facebook_id) not even when I open it directly on the browser, at least not the entire list. This is what it shows on the browser
{
"data": [
{
"name": "Face_id_name",
"id": "facebook_numeric_id"
}
],
"paging": {
"next": "https://graph.facebook.com/v2.2/facebook_id/friends?access_token=authentic_accesstoken&limit=25&offset=25&__after_id=enc_Some_encrypted_code"
},
"summary": {
"total_count": 263
}
}
In data it doesn't show the entire list and when I use link to paging: next: it doesn't give me anything just total count again.
I am not entirely sure whether my url is right or not.

You can´t get the friends of ANY user, you can only get the friends of the authorized user and only those who authorized the App too - for privacy reasons. So this is the only correct call:
https://graph.facebook.com/me/friends?access_token=authentic_accesstoken
There is no need to use the ID, because you can only use the ID of the authorized user anyway. So you can just use "me" instead.
More information about the limited friend result can be found in countless other threads, for example: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app

Related

Is there a module/method in Python to help identify if a Google Account is a regular account or a Google Workspace-linked account?

A little more detail on the question -
Scenario
The app I'm working on currently performs the following -
Logs in users via Google OAuth ( added to Auth0 login )
Comprises of a list of Google Sheets with their links, which the user can open when he is logged in
When the user clicks on a sheet's link to open it, he is redirected to a page where the sheet is expected to be displayed in an iframe.
The gspread module in Python retrieves the list of users the sheet has been shared with (permission list) (gspread is authenticated using a service account which helps do this). If the authenticated user is a part of the permission list, the iframe is displayed, else, an error message is displayed.
Now, the next requirement we'd like to achieve is for specific users in the site to be able to share the Google Sheet with other users, using the share method in the gspread module. However, we would like to share it with users with regular Google accounts, and not those enabled with Google Workspace, owing to business requirements which I prefer not to disclose at this point.
Is there a way to do this? I've found a something here - https://developers.google.com/admin-sdk/directory/v1/quickstart/python#configure_the_sample, but this is only to check with the users of the same workspace, if the service account I possess is that of the workspace's admin, but what I need to know is in general if a given account is a regular one or is linked to the workspace of any organization.
The People api has a method called people.get If i pass it me and check the person fields for memberships
Workspace domain account
{
"resourceName": "people/106744391248434652261",
"etag": "%EgMBLjcaBAECBQciDFpMNzJsdkk3SG80PQ==",
"memberships": [
{
"metadata": {
"source": {
"type": "DOMAIN_PROFILE",
"id": "106744391248434652261"
}
},
"domainMembership": {
"inViewerDomain": true
}
}
]
}
standard gmail user
{
"resourceName": "people/117200475532672775346",
"etag": "%EgMBLjcaBAECBQciDEdwc0JEdnJyNWRnPQ==",
"memberships": [
{
"metadata": {
"source": {
"type": "CONTACT",
"id": "3faa96eb08baa4be"
}
},
"contactGroupMembership": {
"contactGroupId": "myContacts",
"contactGroupResourceName": "contactGroups/myContacts"
}
}
]
}
So the answer is yes you need to go though the google people api. I dont have any python examples for the people api on hand but let me know if you cant get it working.

Is there a way to obtain Facebook Insights for specific posts trough the Graph API?

Sorry to bother with maybe a stupid question, but I'm still a beginner with the Graph API. A little background to better understand my question: I need to run an analysis on a Facebook page (of which I'm not the owner but administrator, small size, ~4000 likes and ~150 posts, more or less one per day). What I intended to do was the following:
Obtain the data trough Graph API. Namely, I was most interested in retrieving the messages, number of likes and reach of every post
Import the data in R and identify the outliers (I mean, the posts whose likes and reach are not in line with the mean)
Look for correlations between those messages (since the page, for its nature, needs to talk about a wide range of topics, I want to understand which of them generate the most reactions and plan accordingly)
I've already done an analysis "by hand", but I want to test if it is possible to make the same conclusions without involving a human operator.
I've looked on the web for tutorials on how to use the graph API in python, but I've not been able to find something comprehensive. I've set up my API and obtained the permanent page token with manage_pages and read_insights permissions.
Here an idea of what I'm doing:`
def get_facebook_page_data(page_id, access_token):
website = "https://graph.facebook.com/v3.1/"
location = "%s/posts/" % page_id
fields = "?fields=message,id" + \
"reactions.type(LIKE).limit(0).summary(total_count).as(reactions_like)"
authentication = "&limit=100&access_token=%s" % (access_token)
request_url = website + location + fields + authentication
data = json.loads(request_data_from_url(request_url))
return data`
So, with this function I'm able to obtain the id, message and number of likes of all the posts stored inside data, and with another function I write everything on a csv file.
First question: am I doing something wrong?
Second question: I cannot retrieve a lot of information. For example, when adding type to the fields, it says that this is deprecated (I'm running python 3.7.3)
Third questions: how do I retrieve the reach for every post? I'm assuming this is obtained by scraping the insights, by I don't seem to get it right... How do I query the Graph API for those data?
In general, I'm finding a lot of trouble in just getting the right keywords while building the links. I've installed facebook-sdk but I don't know how to use it (as I said, I'm a beginner). Do you have suggestions on this?
Thanks very much to everyone answering, and greetigs from Italy!
First of all I suggest to use the latest version of the API available, currently the 5.0, regarding your question:
Second question: I cannot retrieve a lot of information. For example,
when adding type to the fields, it says that this is deprecated (I'm
running python 3.7.3)
Regarding to the doc of the Page Feed see the attachments field, as example, adding this to the request:
attachments.fields(media_type)
Third questions: how do I retrieve the reach for every post? I'm
assuming this is obtained by scraping the insights, by I don't seem to
get it right... How do I query the Graph API for those data?
Regarding to the doc of the Page Insights see the page_impressions field, as example in order to return the page_impressions field for a lifetime period:
insights.period(lifetime).metric(post_impressions_unique)
A complete example:
https://graph.facebook.com/v3.1/<PAGE-ID>/posts?fields=message,id,reactions.type(LIKE).limit(0).summary(total_count).as(reactions_like),insights.period(lifetime).metric(post_impressions_unique),attachments.fields(media_type)
Will return:
{
"data": [{
"message": "Hello",
"id": "269816000129666_780829305694997",
"reactions_like": {
"data": [],
"summary": {
"total_count": 0
}
},
"insights": {
"data": [{
"name": "post_impressions_unique",
"period": "lifetime",
"values": [{
"value": 15
}],
"title": "Lifetime Post Total Reach",
"description": "Lifetime: The number of people who had your Page's post enter their screen. Posts include statuses, photos, links, videos and more. (Unique Users)",
"id": "269816000129666_780829305694997/insights/post_impressions_unique/lifetime"
}],
"paging": {
"previous": "https://graph.facebook.com/v3.1/269816000129666_780829305694997/insights?access_token=EAAAAKq6xRNcBAOMKY3StjWXPgL1REATIfPFsyZCY21KDAnZAZB7MpKgNGCHRlKVt9bZBoVZAHpV0jqxZAAVZCOKDIh96YxvpxPaavR1AYK5EQCEEOSMKqz4ZAItcX9WvVfEEN5FzqgyoQWi8oKZBQmQB4Nf80SgicaesluNbI0hDMw2QAxfV9rAFpRc10Pop1d1vtVeziPEjEKwZDZD&metric=post_impressions_unique&period=lifetime&since=1573891200&until=1574064000",
"next": "https://graph.facebook.com/v3.1/269816000129666_780829305694997/insights?access_token=EAAAAKq6xRNcBAOMKY3StjWXPgL1REATIfPFsyZCY21KDAnZAZB7MpKgNGCHRlKVt9bZBoVZAHpV0jqxZAAVZCOKDIh96YxvpxPaavR1AYK5EQCEEOSMKqz4ZAItcX9WvVfEEN5FzqgyoQWi8oKZBQmQB4Nf80SgicaesluNbI0hDMw2QAxfV9rAFpRc10Pop1d1vtVeziPEjEKwZDZD&metric=post_impressions_unique&period=lifetime&since=1574236800&until=1574409600"
}
},
"attachments": {
"data": [{
"media_type": "photo"
}]
}
},
{
"message": "Say hello!",
"id": "269816000129666_780826782361916",
"reactions_like": {
"data": [],
"summary": {
"total_count": 0
}
},
"insights": {
"data": [{
"name": "post_impressions_unique",
"period": "lifetime",
"values": [{
"value": 14
}],
"title": "Lifetime Post Total Reach",
"description": "Lifetime: The number of people who had your Page's post enter their screen. Posts include statuses, photos, links, videos and more. (Unique Users)",
"id": "269816000129666_780826782361916/insights/post_impressions_unique/lifetime"
}],
"paging": {
"previous": "https://graph.facebook.com/v3.1/269816000129666_780826782361916/insights?access_token=EAAAAKq6xRNcBAOMKY3StjWXPgL1REATIfPFsyZCY21KDAnZAZB7MpKgNGCHRlKVt9bZBoVZAHpV0jqxZAAVZCOKDIh96YxvpxPaavR1AYK5EQCEEOSMKqz4ZAItcX9WvVfEEN5FzqgyoQWi8oKZBQmQB4Nf80SgicaesluNbI0hDMw2QAxfV9rAFpRc10Pop1d1vtVeziPEjEKwZDZD&metric=post_impressions_unique&period=lifetime&since=1573891200&until=1574064000",
"next": "https://graph.facebook.com/v3.1/269816000129666_780826782361916/insights?access_token=EAAAAKq6xRNcBAOMKY3StjWXPgL1REATIfPFsyZCY21KDAnZAZB7MpKgNGCHRlKVt9bZBoVZAHpV0jqxZAAVZCOKDIh96YxvpxPaavR1AYK5EQCEEOSMKqz4ZAItcX9WvVfEEN5FzqgyoQWi8oKZBQmQB4Nf80SgicaesluNbI0hDMw2QAxfV9rAFpRc10Pop1d1vtVeziPEjEKwZDZD&metric=post_impressions_unique&period=lifetime&since=1574236800&until=1574409600"
}
},
"attachments": {
"data": [{
"media_type": "photo"
}]
}
},

Public photo albums & Facebook Graph API

It seems that the Facebook graph API can not always pull down the data for public albums, even when you provide a valid access token, but you can see these albums through a standard http GET request to facebook.com.
Using the following python code, I am able to GET data from the public album "303027476429477":
>>> r = requests.get('https://graph.facebook.com/303027476429477?access_token=MY_SECRET_ACCESS_TOKEN',headers=headers)
>>> r.text
u'{\n "created_time": "2012-03-11T23:01:28+0000",\n "name": "Kid Gaga",\n "id": "303027476429477"\n}'
However, for another public album "377622310155", I cannot:
>>> r = requests.get('https://graph.facebook.com/377622310155?access_token=MY_SECRET_ACCESS_TOKEN',headers=headers)
>>> r.text
u'{\n "error": {\n "message": "Unsupported get request. Object with ID \'377622310155\' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",\n "type": "GraphMethodException",\n "code": 100,\n "fbtrace_id": "HK87oTbeTs0"\n }\n}'
This seems to be a permissions issue, since if one simply visits http://facebook.com/377622310155 in their browser, they can see the album does exist (and they didnt need to do any authentication to see it either).
So my question is - what am I doing wrong, and since all i want to do is know if an albumn exists or not, should I just scrape facebook.com's http response?

Getting my wall posts or friends from Facebook - lists are empty

I performed following:
installed 2 kinds of the Facebook APIs: pip install facebook and pip install fb
Went to https://developers.facebook.com/tools/access_token/, registered my app and got User Token (some long string, used currently as hardcoded in the code, I'll change it later)
Created the following code (please see comments what and how works or doesn't:
import re
import urllib2
import requests
import facebook
import fb
token="User Token"
graph = facebook.GraphAPI(token)
profile = graph.get_object("me") #POSITIVE: I can get profile info
friends = graph.get_connections('me', "friends") #POSITIVE: I can get real friends count, NEGATIVE - the list itself is empty
posts = graph.get_connections('me', "posts") #NEGATIVE - the list itself is empty, no count is returned, as in "friends" case
feed= graph.get_connections('me', "feed") #NEGATIVE - the list itself is empty, no count is returned, as in "friends" case
fb1=fb.graph.api(token)
profile = fb1.get_object(cat='single', id="me") #NEGATIVE - error in given parameters
To be sure I used also https://developers.facebook.com/tools/explorer with the same token and got the same results:
GET me\friends:
{
"data": [
],
"summary": {
"total_count": 575
},
}
GET me\feed:
{
"data": [
],
}
Why the returned lists are empty?
Thanks!
Update: I found that I have no permissions, only those. How to get more? and why with permission "friends" I can't see info about them, but only total number of them?
{
"data": [
{
"permission": "user_friends",
"status": "granted"
},
{
"permission": "public_profile",
"status": "granted"
}
],
}
Resolved: I found where the problem! I didn't get app token with all permissions. Perform the following:
Go to the https://developers.facebook.com/tools/explorer
Choose your APP name (my is lj_for_me in attached image)
Press "Get User Token" and check all boxes (or what you really need).
Now you APP will have permission to get post and photos and etc from YOUR FB account. For getting the same from other users accounts you need to get such permission from FB (as far as I understood, but I did not enter too deep into this subject)
Since v2.0 of the Graph API, you can only get friends who authorized your App with the user_friends permission too, for privacy reasons. You only get the total_count if none of your friends authorized the App.
Access to wall posts is possible with the user_posts permission, btw.

Python facebook-sdk: retrieving names of users who posted a message on a page

I want to use the Python facebook-sdk library to retrieve the names of the persons who posted a message on a Facebook page I created.
This is an example, returned by the Graph API explorer:
{
"feed": {
"data": [
{
"from": {
"name": "Ralph Crützen",
"id": "440590514975673"
},
"message": "Nog een test.",
"created_time": "2015-10-17T19:33:30+0000",
"id": "649463214822976_649745285127205"
},
{
"from": {
"name": "Ralph Crützen",
"id": "440590514975673"
},
"message": "Testing!",
"created_time": "2015-10-16T20:44:17+0000",
"id": "649463214822976_649492455153388"
},
... etc ...
But when I use the following Python code...
graph = facebook.GraphAPI(page_access_token)
profile = graph.get_object('tinkerlicht')
posts = graph.get_connections(profile['id'], 'feed')
print(posts['data'][0]['message'])
print(posts['data'][0]['from']['name'])
...only the message value is printed. Printing the name of the person who posted the message gives the error:
print(posts['data'][0]['from']['name'])
KeyError: 'from'
At first, I thought that I needed the read_page_mailboxes permission. To use this permission, it has to be approved by Facebook, so I submitted a request. But Facebook replied:
"You don't need any additional permissions to post to Pages or blogs that you administer. You only need to submit your app for review if your app will use a public-facing login."
So what exactly is the reason I can't retrieve the from data from the messages feed? (While reading it from the Graph API explorer works fine...)
Btw, I'm using a page access token which never expires. I generated this token the way it's described here.

Categories