Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain a user access token in Python

I'm using the unofficial python sdk for Facebook. This works fine for alot of graph api calls, but my recent project requires using more FQL. Certain tables (notifications for example) require a user access token rather than an app access token.

When using

graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_A‌​PP_SECRET&grant_type=client_credentials 

it seems I only get an App token back. So when calling the notification table I'll receive a "102 : A user access token is required to request this resource"

This project does not use a browser at all, and is mainly created to validate notifications, and postings generated from another app.

Whats the best way to receive a user token in this case?

Thanks N

like image 417
blotto Avatar asked Dec 13 '22 02:12

blotto


1 Answers

You can not get a user access token without a direct interaction of a logged in user with your application using the facebook authentication flows. The only token that can be obtained without a user is the application token and you've got that covered already.

You can create a user token manually, the easiest way, I think, would be to use the Graph Explorer tool. Just select your application on the top right, then click the "Get Access Token" button, you can then check the needed permissions and after that a generated token will appear in the text box, copy that and use it.

The problem with that is that the access token is a short lived one (a few hours), if you need a longer one I suggest that you create a canvas application that will only implement the Server-Side authentication which will end with a long-lived token (60 days), then you can persist it and use it for that time.

like image 88
Nitzan Tomer Avatar answered Dec 23 '22 11:12

Nitzan Tomer