Try to operate facebook using Python
** Demo page ** https://needtec.sakura.ne.jp/check_facebook/
Github https://github.com/mima3/check_facebook
facebook-sdk facebook-sdk is an API that operates the Facebook Graph API in Python.
https://github.com/pythonforfacebook/facebook-sdk
easy_install facebook-sdk
bottle Python web framework. Installation is easy because it consists of only one file.
http://bottlepy.org/docs/dev/index.html
beaker Library for session management in Python
https://beaker.readthedocs.org/en/latest/#
easy_install Beaker
** Developer registration procedure ** http://fb.dev-plus.jp/what-devplus/dev_register/
Select [Website] as the application type
Enter the application name and select Create New Facebook APP ID
Select a category and enter the Create APP ID.
If you scroll down the created page, you can enter the "Site URL", so enter it and click "Next".
The redirect_url specified in the Facebook API must be the domain name specified in this Sai. It seems that you cannot specify the IP address, so if you want to run it locally, set it to localhost.
After reloading the page, you will be able to select the created application from the menu.
When you select an app, you can see the "App ID" and "App Secret". You can use this value to authenticate and get an access token.
The method of obtaining an access token is shown below.
end point: https://www.facebook.com/dialog/oauth
** Parameters: ** client_id: app ID of facebook app redirect_url: The redirect URL after authentication. An error will occur if the domain is not set. scope: Specify permissions separated by ",". https://developers.facebook.com/docs/facebook-login/permissions/v2.2?locale=ja_JP
** Example: ** https://www.facebook.com/dialog/oauth?client_id=XXXXX&redirect_uri=http%3A%2F%2Flocalhost%2Fcheck_facebook&scope=read_stream
Redirect URL when OK
https://localhost/check_facebook/index.cgi/?code=XXXXX#_=_
Redirect URL in case of cancellation
https://localhost/check_facebook/?error=access_denied&error_code=200&error_description=Permissions+error&error_reason=user_denied#_=_
end point: https://graph.facebook.com/oauth/access_token
** Parameters: ** client_id: app ID of facebook app client_secret: facebook app Secret redirect_url: The redirect URL after authentication. An error will occur if the domain is not set. code: Code obtained by oauth
** Example: ** https://graph.facebook.com/oauth/access_token?client_id=facebookアプリのAppID&client_secret=facebookアプリのSecret&redirect_uri=http%3A%2F%2Flocalhost%2Fcheck_facebook&code=oauthで取得したCode
In case of error:
{
"error": {
"message": "Error validating application. Invalid application ID.",
"type": "OAuthException",
"code": 101
}
}
If you can get access_token:
access_token=XXX&expires=5183979
Access Graph API using access_token obtained here
Below is a sample when bottle is operated with CGI.
index.cgi
from bottle import run
from application import app
from beaker.middleware import SessionMiddleware
session_opts = {
'session.type': 'file',
'session.data_dir': './session',
'session.cookie_expires': True,
'session.auto': True
}
appSession = SessionMiddleware(app, session_opts)
run(appSession, server='cgi')
application.py
from bottle import get, post, template, Bottle, response, request, redirect
import os
app = Bottle()
@app.get('/')
def index():
session = request.environ.get('beaker.session')
session['counter'] = session.get('counter', 0) + 1
session.save()
return template('<b>Hello {{name}}</b>!', name=session['counter'])
The session.data_dir file is updated each time the page is accessed. Beaker does not delete the created file, so delete it regularly with cron etc.
find /hoge/session -type f -mmin +60 -exec rm {} \;
# -*- coding: utf-8 -*-
import facebook
graph = facebook.GraphAPI('Obtained API')
profile = graph.get_object('facebook page ID or name')
print profile
posts = graph.get_connections(profile['id'], 'posts')
print posts
By using facebook-sdk, bottle, and Beaker, you can create applications using facebook API even in Python.
Recommended Posts