Home > Enterprise >  how to upload reels to facebook with graph api using python
how to upload reels to facebook with graph api using python

Time:11-11

I am trying to upload reel with Graph Api using Python. I'm getting an error every time I try to upload video.

Error :

{"debug_info":{"retriable":false,"type":"NotAuthorizedError","message":"User not authorized to perform this request"}}

Note: I have given every possible permission to my app and page.

code:

import requests
import os
import json

Title ="Title of the video"
title = Title
description = title

source = f"F:\proj_ytTofb\downloads\{Title}.mp4"
files = {'source': open(source, 'rb')}
file_size = os.path.getsize(source)
print("File Size is :", file_size, "bytes")

def Initialize():
    url = f"https://graph.facebook.com/v13.0/{page_id}/video_reels?upload_phase=start"
    payload = {
        'access_token': token,
    }
    r = requests.post(url, data = payload)
    return r.json()["video_id"]


video_id=Initialize()
print(video_id)

def Upload():
    url = f"https://rupload.facebook.com/video-upload/v15.0/{video_id}"
    payload ={
        'access_token': token,
        'offset': 0,
        'file_size': file_size,
    }
    r = requests.post(url, files=files, data=payload)
    return r.text

print(Upload())

output: {"debug_info":{"retriable":false,"type":"NotAuthorizedError","message":"User not authorized to perform this request"}}

CodePudding user response:

Your Upload code seem bit wrong if you refer on documentation

def Upload(vidid, size, filedata):
    url = f"https://rupload.facebook.com/video-upload/v13.0/{vidid}"
    payloadUp = {
        'Authorization': 'OAuth '   page_access_token,
        'offset': "0",
        'file_size': str(size),
    }

    print(payloadUp)
    r = requests.post(url, data=filedata, headers=payloadUp)
    return r.text

with parameter like this

files = {'source': open(mp4_path, 'rb')}
file_size = os.path.getsize(mp4_path)

and then you called it like this

Upload(video_id, file_size, files)

Note: I successfully upload it on fb reels and published it, but I dont what happen the video failed to convert without error notice.

  • Related