27
loading...
This website collects cookies to deliver better user experience
python script.py -ai app_id -cs client_secret -pi page_id
.import requests
import argparse
API_URL = 'https://graph.facebook.com'
def parse_args(publish_post):
parser = argparse.ArgumentParser()
parser.add_argument('-pid', '--pageId', dest='pageid', action='store',
required=True,
help='page id')
parser.add_argument('-cs', '--clientsecret', dest='clientsecret', action='store',
required=True,
help='client secret token')
parser.add_argument('-ai', '--appid', dest='appid', action='store',
required=True,
help='the id of the app')
client_secret = parser.parse_args().clientsecret
page_id = parser.parse_args().pageid
app_id = parser.parse_args().appid
return publish_post(app_id, client_secret, page_id)
def run():
@parse_args
def publish_post(app_id, client_secret, page_id):
params = {
'access_token': f'{app_id}|{client_secret}',
'scope': 'pages_manage_posts,public_profile' # permissions
}
url = f'{API_URL}/device/login'
res = requests.post(url, params=params).json()
interval = res['interval']
# poll for login
# check if the user authorized our app or not
data = {
'access_token': f'{args.id}|{args.token}',
'code': res['code']
}
url = f'{API_URL}/device/login_status'
while True:
res = requests.post(url, data=data).json()
try:
access_token = res['access_token']
break
except KeyError:
continue
sleep(10)
# generate a page access token
params = {
'fields': 'access_token',
'access_token': access_token,
}
url = f'{API_URL}/{page_id}'
res = requests.get(url, params=params).json()
print('page access token is', res['access_token'])
run()