34
loading...
This website collects cookies to deliver better user experience
109002898292850
./feed
after it , and don't forget to change the type of http request from GET to POST, then add a new parameter name it message, and write something there, message is the text that we are going to publish, make sure to check this link from the official Facebook documentationif you want more details about making an app to publish posts on Facebook pages._
, you can access the post by replacing page_id_post_id with the generated id in facebook.com/page_id_post_id
.publish.py
, then lets write a function that can make the user pass page access token, page id , and the version of the facebook api, as an arguments from the command line, we will use the parse_args
function as a decorator, then we can add another function which gonna handle publishing posts to the desired page.# publish.py
import requests
import argparse
API_URL = 'https://graph.facebook.com'
def parse_args(publish_post):
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--token', dest='token',
action='store',
required=True,
help='page access token')
parser.add_argument('-id', '--pageId', dest='page_id',
action='store',
required=True,
help='page id')
parser.add_argument('-v', '--version', dest='version', action='store',
required=False,
help='version of the api')
token = parser.parse_args().token
page_id = parser.parse_args().page_id
version = parser.parse_args().version
return publish_post(token, page_id, version)
@parse_args
def publish_post(token, page_id, version):
pass
get_api_url
, this function have only a one parameter named version with a None as a it's default value, and add this function to publish_post
and assign it to a variable name url.@parse_args
def publish_post(token, page_id, version):
url = get_api_url(version)
def get_api_url(version=None):
if version:
return api_url+'/v'+str(version)
return api_url
/{page_id}/photos
, if you want read more make sure to check this link of the official Facebook documentation.url = f'{get_api_url(version)}/{page_id}/photos'
if message:
payload = {
'access_token': token,
'message': message
}
else:
payload = {
'access_token': token,
}
photo = open(photo_path, 'rb')
files = {
'data': photo
}
response = requests.post(url, data=payload,
files=files).json()
print(response)
parser.add_argument('-m', '--message', dest='message',
action='store',
required=True,
help='facebook post text')
parser.add_argument('-p', '--photo', dest='photo',
action='store',
required=True,
help='photo path')
token = parser.parse_args().token
page_id = parser.parse_args().page_id
version = parser.parse_args().version
message = parser.parse_args().message
photo = parser.parse_args().photo
return publish_post(token, page_id, version, message, photo)
import requests
import argparse
API_URL = 'https://graph.facebook.com'
def parse_args(publish_post):
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--token', dest='token', action='store',
required=True,
help='page access token')
parser.add_argument('-id', '--pageId', dest='page_id', action='store',
required=True,
help='page id')
parser.add_argument('-v', '--version', dest='version', action='store',
required=False,
help='version of the api')
parser.add_argument('-m', '--message', dest='message', action='store',
required=True,
help='facebook post text')
parser.add_argument('-p', '--photo', dest='photo', action='store',
required=True,
help='photo path')
token = parser.parse_args().token
page_id = parser.parse_args().page_id
version = parser.parse_args().version
message = parser.parse_args().message
photo = parser.parse_args().photo
return publish_post(token, page_id, version, message, photo)
def get_api_url(version=None):
if version:
return API_URL+'/v'+str(version)
return API_URL
def run():
@parse_args
def publish_post(token, page_id, version, message, photo_path):
url = f'{get_api_url(version)}/{page_id}/photos'
if message:
payload = {
'access_token': token,
'message': message
}
else:
payload = {
'access_token': token,
}
photo = open(photo_path, 'rb')
files = {
'data': photo
}
response = requests.post(url, data=payload, files=files).json()
print(response)
run()
python facebook_page_publish.py -t {page_access_token} -id {page_id} -m {post_text} -p {photo_path}
{'id': 'photo_id', 'post_id': 'page_id_post_id'}
, indicate that you did something wrong, try to fix it or leave a comment below maybe i can help.