-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.py
54 lines (37 loc) · 1.65 KB
/
facebook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/python2.7
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from json import dumps
from oauthlib.common import urldecode
from requests_oauthlib import OAuth2Session
import requests
from settings import FACEBOOK
AUTHORIZATION_BASE_URL = 'https://www.facebook.com/dialog/oauth'
TOKEN_URL = 'https://graph.facebook.com/oauth/access_token'
REDIRECT_URI = 'https://pokr.kr/'
POST_URL = "https://graph.facebook.com/%s/feed"
def facebook_compliance_fix(session):
def _compliance_fix(r):
token = dict(urldecode(r.text))
token['expires_in'] = token['expires']
token['token_type'] = 'Bearer'
r._content = dumps(token)
return r
session.register_compliance_hook('access_token_response', _compliance_fix)
return session
def setup_oauth():
oauth = OAuth2Session(FACEBOOK['CLIENT_ID'], redirect_uri=REDIRECT_URI, scope=["manage_pages","publish_stream"])
oauth = facebook_compliance_fix(oauth)
authorization_url, state = oauth.authorization_url(AUTHORIZATION_BASE_URL)
print 'Please go here and authorize,', authorization_url
redirect_response = raw_input('Paste the full redirect URL here:')
token = oauth.fetch_token(TOKEN_URL, client_secret=FACEBOOK['CLIENT_SECRET'],
authorization_response=redirect_response)
return token["access_token"]
def get_oauth():
oauth = OAuth2Session(FACEBOOK['CLIENT_ID'], redirect_uri=REDIRECT_URI)
oauth = facebook_compliance_fix(oauth)
return oauth
def post(message):
oauth = get_oauth()
oauth.post(url=POST_URL % FACEBOOK['PAGE_ID'], data={"message":message,"access_token":FACEBOOK['OAUTH_TOKEN']})