Search code examples
pythonfacebookvideoupload

How to upload reels to Facebook with graph API using Python


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"}}


Solution

  • 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.