How to update FB status using Python & GraphAPI?

This question was asked before, but many of the solutions were outdated, and the GraphAPI requirement seemed to make many solutions inappropriate. I played around with fbpy, facebook, oauth, oauth2 files and looked at their examples, but still can't figure out how to make it work. I trust neither the code nor the packages that I used, and I wonder if anyone will have any final solutions that they know will work. Thank you

+4
source share
4 answers

First you need to understand what the stream login is . You need to understand if you want to easily switch between different Facebook libraries. Therefore, it may have code that is very verbose for code that is very implementation-based.

The following is that there are various ways to implement OAuth processing and various ways to display and run your web application in Python. It is not possible to authorize without entering the browser. Otherwise, you will need to save a copy of the access_token code in the code.

Suppose you select web.py to handle the presentation of your web application and requests.py to handle the HTTP calls to the Graph API.

 import web, requests 

Then configure the URL where we want all requests to go through

 url = ( '/', 'index' ) 

Now get your application id, secret address and url after login you want to use

 app_id = "YOUR_APP_ID" app_secret = "APP_SECRET" post_login_url = "http://0.0.0.0:8080/" 

This code will have one index class to handle logic. In this class we want to deal with authorization code Facebook will return after logging in

Login flow

 user_data = web.input(code=None) code = user_data.code 

Set the condition for checking code

 if not code: # we are not authorized # send to oauth dialog else: # authorized, get access_token 

In the "without authorization" branch, send the user to the dialog box

 dialog_url = ( "http://www.facebook.com/dialog/oauth?" + "client_id=" + app_id + "&redirect_uri=" + post_login_url + "&scope=publish_stream" ) return "<script>top.location.href='" + dialog_url + "'</script>" 

Otherwise, we can extract the access_token using the code received

 token_url = ( "https://graph.facebook.com/oauth/access_token?" + "client_id=" + app_id + "&redirect_uri=" + post_login_url + "&client_secret=" + app_secret + "&code=" + code ) response = requests.get(token_url).content params = {} result = response.split("&", 1) for p in result: (k,v) = p.split("=") params[k] = v access_token = params['access_token'] 

Here you can choose how you want to deal with the call in order to update the status, for example, a form,

 graph_url = ( "https://graph.facebook.com/me/feed?" + "access_token=" + access_token ) return ( '<html><body>' + '\n' + '<form enctype="multipart/form-data" action="' + graph_url + ' "method="POST">' + '\n' + 'Say something: ' + '\n' + '<input name="message" type="text" value=""><br/><br/>' + '\n' + '<input type="submit" value="Send"/><br/>' + '\n' + '</form>' + '\n' + '</body></html>' ) 

Or using face.py

 from facepy import GraphAPI graph = GraphAPI(access_token) try: graph.post( path = 'me/feed', message = 'Your message here' ) except GraphAPI.OAuthError, e: print e.message 

So in the end you can get a smaller version, for example

 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=publish_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.post(path = 'me/feed', message = 'Your message here') 

See details

* Facebook API - Custom feed: http://developers.facebook.com/docs/reference/api/user/#feed
* Post a Facebook photo to Python - Basic Sauce: http://philippeharewood.com/facebook/publish-a-facebook-photo-in-python-the-basic-sauce/
* Facebook and Python - Basic Sauce: http://philippeharewood.com/facebook/facebook-and-python-the-basic-sauce/

+4
source

One of the possible (proven!) Solutions using facepy :

  • Create a new application or use an existing previously created one.
  • Create a user access token using the GUI API with extended status_update permission for the application.
  • Use the user access token created in the previous step using facepy :

     from facepy import GraphAPI ACCESS_TOKEN = 'access-token-copied-from-graph-api-explorer-on-web-browser' graph = GraphAPI(ACCESS_TOKEN) graph.post('me/feed', message='Hello World!') 
+3
source

You can try this blog. It uses the fbconsole application.

Code from the blog:

 from urllib import urlretrieve import imp urlretrieve('https://raw.github.com/gist/1194123/fbconsole.py', '.fbconsole.py') fb = imp.load_source('fb', '.fbconsole.py') fb.AUTH_SCOPE = ['publish_stream'] fb.authenticate() status = fb.graph_post("/me/feed", {"message":"Your message here"}) 
+2
source

This is how I earned it. You absolutely do not need to create applications for this. I'll tell you how to post status updates on your profile and on your facebook page.

First, to post a status update to your profile:

Go to https://developers.facebook.com/tools/explorer .
Before that, you will see a text box with an access token. Click on the "Get Access Token" button next to this text box. It will open a popup asking you to grant you various permissions for the access token. Basically, these permissions determine that everything you can do through the Graph API using this token. Check the boxes next to all the necessary permissions that will update your status.
Now go and install the personal module. A better way would be to use pip install.
After that, pase the following code snippet in any .py file:

 from facepy import GraphAPI access_token = 'YOUR_GENERATED_ACCESS_TOKEN' apiConnection = GraphAPI(access_token) apiConnection.post(path='me/feed', message='YOUR_DESIRED_STATUS_UPDATE_HERE') 

Now run this .py file in the standard python way and check your facebook. You should see YOUR_DESIRED_STATUS_UPDATE_HERE posted to your facebook profile.

Next, to do the same with your facebook page:

The procedure is almost exactly the same, except for generating your access token.
Now you can’t use the same access token to publish on your facebook page. You need to generate a new one, which can be a little complicated for someone new in the Graph API. Here is what you need to do:

Go to the same page developers.facebook.com/tools/explorer.

Find the drop-down list showing the "GUI API" and click on it. In the drop-down list, select your page on which you want to publish updates. Create a new access token for this page. The process is described here :. Remember to check the manage_pages permission on the advanced permissions tab.

Now use this token in the same code that was used earlier, and run it.

Go to your Facebook page. You must send YOUR_DESIRED_STATUS_UPDATE to your page.

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1445915/


All Articles