Obtaining a facebook access token to use with facepy - facebook

I am doing a project for school where I have to get all of my friend data, and the friend data of some of my friends, from facebook in order to make a graph of it. To do this I am planning on using facepy, but in order to do that I need an access token. My question is how do I obtain this access token?

facepy doesn't natively include a way for the OAuth process
https://github.com/jgorset/facepy/issues/22
You will need to use your own method or external library for the user to be guided via a web application.
For example using web.py and facepy to get me/posts with read_stream permission
import web
from facepy import GraphAPI
from urlparse import parse_qs
url = ('/', 'index')
app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"
user_data = web.input(code=None)
if not user_data.code:
dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
"client_id=" + app_id +
"&redirect_uri=" + post_login_url +
"&scope=read_stream" )
return "<script>top.location.href='" + dialog_url + "'</script>"
else:
graph = GraphAPI()
response = graph.get(
path='oauth/access_token',
client_id=app_id,
client_secret=app_secret,
redirect_uri=post_login_url,
code=code
)
data = parse_qs(response)
graph = GraphAPI(data['access_token'][0])
graph.get('me/posts')
For more info see
* Facebook API - User Posts: http://developers.facebook.com/docs/reference/api/user/#posts
* Publish a Facebook Photo in Python – The Basic Sauce: http://philippeharewood.com/facebook/publish-a-facebook-photo-in-python-the-basic-sauce/
* Facebook and Python – The Basic Sauce: http://philippeharewood.com/facebook/facebook-and-python-the-basic-sauce/

Basically, you just have to log in.
If you go to the graph API overview page It will ask you to log in with your own facebook account. After that all of the links on the page will have an access token with permission to see whatever you can see.

Related

Access token API Graph Facebook

I use Angular 2 and 4 in order to show Facebook events and attending on my website.
if(link.match('^http://www.facebook.com/|https://www.facebook.com/')){
console.log("Facebook link")
id = link.split('https://www.facebook.com/')[1].split('?')[0];
fblink = this.http.get(`https://graph.facebook.com/${id}?fields=events.limit(1000){id,name,description,cover,start_time,end_time,attending_count,place,photos{source},attending{first_name,last_name,cover}}&access_token=MYACCESSTOKEN`)
.map(events => events.json().events.data);
}
This example works because I use the id of a Facebook page. But when I want to use the search option the access token doesn't works.
fblink = this.http.get(`https://graph.facebook.com/v2.9/search?q=${id}&fields=id,name,place,cover,attending_count,start_time,description&type=event&access_token=MYACCESSTOKEN`)
.map(events => events.json().data);
But when I put the access token from the Facebook API Graph tool it works and this token stay about 1 hour and I have to retake the access token from the API Graph tool.
Is there a solution? I don't use any SDK from Facebook maybe there is a solution.
Thank you for your help.

Identifying a Facebook user id from a url?

I've been playing around with the Facebook graphs api for a while, and to the best of my knowledge you shouldn't be able to get a Facebook user's id from their username.
I then came accross stalkscan.com and note that they do just that. You provide a Facebook url and the resulting queries contain that usernames profile id.
How can I acheive a similar result with or without the graphs api?
You can get a facebook user id by using the following endpoint:
URL /?id={url}
{url} represents an external URL as it relates to the Facebook social graph - shares and comments from the URL on Facebook, and any Open Graph objects associated with the URL. (e.g. a user, a page...).
With Python, you can use something like this:
import requests
USER_URL = '' # your link
ACCESS_TOKEN = '' # your credentials
params= {'id': USER_URL,'access_token': ACCESS_TOKEN}
fb_graph = "https://graph.facebook.com/v2.7/"
r = requests.get(fb_graph, params=params)
fb_id = r.json().get('id')
print('Facebook User ID: %s' % fb_id)
Full documentation of the graph API is available here.

Facebook .NET SDK -> Get and Post using generated token

I am writing a Azure Service that will occasionally write to my facebook page as a status. Since the service does not have a UI component, a majority of the examples on the Facebook and Facebook .NET SDK pages are not helpful.
I created an application on facebook and then fired up the F# REPL in Visual Studio. I generated the token like so:
#r "../packages/Facebook.7.0.6/lib/net45/Facebook.dll"
#r "../packages/Newtonsoft.Json.7.0.1/lib/net45/Newtonsoft.Json.dll"
open Facebook
open Newtonsoft.Json
type Credentials = {client_id:string; client_secret:string; grant_type:string;scope:string}
let credentials = {client_id="859968674039398";
client_secret="XXXXXXXXXX";
grant_type="client_credentials";
scope="manage_pages,publish_stream,read_stream,publish_checkins,offline_access"}
let client = FacebookClient()
let tokenJson = client.Get("oauth/access_token",credentials)
type Token = {access_token:string}
let token = JsonConvert.DeserializeObject<Token>(tokenJson.ToString())
A token comes back as expected. However, when I go to use the token, I am getting errors:
let client' = FacebookClient(token.access_token)
let me = client'.Get("me")
returns
An active access token must be used to query information about the
current user.
and
let pageId = "/me"
type FacecbookPost = {title:string; message:string}
let post = {title="Test Title"; message = "Test Message"}
let postResponse = client'.Post(pageId + "/feed", post)
returns
The user hasn't authorized the application to perform this action
When I read the docs, they talk about getting the application to be approved by Facebook -> but that makes no sense in my use case b/c there is no application as defined with a human end user -> or even any other user invoking the code.
When I generate the token on Facebook Graph Api explorer with the correct permissions, I can use the token to make those GETS and POSTS. Should I just generate the token and stick it in my .config file? How long does a token last?
Thanks in advance
I think you haven't fully understood how Facebook API works.
You always need an App to perform an action (in your case the APP is 859968674039398)
In order to post on behalf a user, you will need that user to grant permissions to your App.
Your App has to be public and if you require more permissions than the basic ones, you need to go through the review process.
The access token you get from the Graph API Explorer (which is an App BTW) is only for you.
Please read the docs CBro provided.
I hope it helps.

How to extend facebook access token in python

I am using the Python facebook-sdk client library.
I currently have a short-lived access token obtained from https://developers.facebook.com/tools/accesstoken/ that I copy the code from the site into my code for authentication.
graph = facebook.GraphAPI(access_token)
This token however expires after 60-mins. I am looking to extend this to a 60 day-long lived token so that don't need to manually copy in new every time it expires. I can find numerous answers on how to do this in different formats, however not python (or at least not simply without log in page etc.).
[for reference, the code I will be using is only intended for my use, and as such, I am not looking to create a log in page. I just want to be able to extend the token I already have].
Not sure if this was available in python's FB API when the question was originally asked, but a neater approach to extend the expiry of the access token would be:
graph = facebook.GraphAPI(user_short_lived_token_from_client)
app_id = 'app_id' # Obtained from https://developers.facebook.com/
app_secret = 'app_secret' # Obtained from https://developers.facebook.com/
# Extend the expiration time of a valid OAuth access token.
extended_token = graph.extend_access_token(app_id, app_secret)
print extended_token #verify that it expires in 60 days
here's an edited version , compatible with latest api versions:
import requests
import json
access_token = 'your token' # Obtained from https://developers.facebook.com/tools/accesstoken/
app_id = "your app id" # Obtained from https://developers.facebook.com/
client_secret = "app secret" # Obtained from https://developers.facebook.com/
link = "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=" + app_id +"&client_secret=" + client_secret + "&fb_exchange_token=" + access_token
s = requests.Session()
token = s.get(link).content
token=json.loads(token)
token=token.get('access_token')
print token
According to their subsection on extending short lived client tokens, you'll need to take in your short lived client token and, having filled in the relevant app data, send a GET request from your server to the following endpoint:
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
The response will contain your long-lived access token which can then be passed back to the client or used on your server. If you don't currently have a module for performing HTTP operations, I highly recommend Requests.

Facebook Graph API get USER token instead of APP token (phonegap)

I'm trying to implement facebook connect in Adobe PhoneGap through the Graph API but apparently I'm getting back an "App" token instead of a "User" token. This causes for my app to disallow any additional users connecting onto it, which is not the point of course.
I have notice this as no matter what user I use for logging in, the access_token returned is always the same.
I'm using the following URL to authenticate:
var authorize_url = "https://graph.facebook.com/oauth/authorize?";
authorize_url += "client_id=" + fb_clientid;
authorize_url += "&redirect_uri=" + fb_redirect_uri;
authorize_url += "&display=" + fb_display;
authorize_url += "&scope=publish_stream"
And to get the authorisation token:
https://graph.facebook.com/oauth/access_token?client_id='+fb_clientid+'&client_secret='+fb_secret+'&code='+fbCode+'&redirect_uri=http://www.facebook.com/connect/login_success.html'
I presume the problem lies with the second url (secret being passed indicates it's an app token) but then how do I get the user token?
Use the Facebook Connect plugin, instead of crafting the login procedure manually. It provides better usability as it is integrated with the Facebook native application or the iOS 6 Facebook functionality.