M
M
Maxim2017-11-29 12:42:31
Python
Maxim, 2017-11-29 12:42:31

How to upload a video to Facebook?

I'm implementing a resumable download, the problem occurs when a chunk is directly applauded.
Option 1, as per the docks

file = open('C:\\Users\\maxim\\Downloads\\ya.mp4', 'rb')

        data = dict(access_token=user.fb_token, upload_phase='transfer',
                    start_offset=int(kwargs.get('start_offset')), upload_session_id=int(kwargs.get('upload_session_id')),
                    video_file_chunk=file
                    )

        # optional destination
        url = '%s%s/videos' % (self.api_video, user.fb_user_id)

        # Headers
        headers = {'Content-type': 'multipart/form-data'}

        req = requests.post(url=url, data=data, headers=headers)

What is stored in data and the url itself
{'start_offset': 0, 'video_file_chunk': <_io.BufferedReader name='C:\\Users\\maxim\\Downloads\\ya.mp4'>, 'upload_phase': 'transfer', 'upload_session_id': 249691982228348, 'access_tok
en': 'EAAXFFwnmYScBABTx6AT40zg1OcQLpSzPd9RBSYAEwqShNvDHLgFCb7A5FnUkvhZAWo0dzhX0Y6NRZADj50JbFqjExR39gnhWGgbjOfqpfiYPsm71NMwKPI94oFmoJf3cWFjHEUAZAwZDZD
'}

'https://graph-video.facebook.com/v2.8/1151655156896/videos'

Response from the server
{'error': {'code': 100, 'message': "Unsupported post request. Object with ID '1151655156896' does not exist, cannot be loaded due to missing permissions, or does not support this o
peration. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api", 'fbtrace_id': 'FEoBntf5jqV', 'error_subcode': 33, 'type': 'GraphMethodException'
}}

Option 2
video_file = {'video_file_chunk': open('C:\\Users\\maxim\\Downloads\\ya.mp4', 'rb')}

        data = dict(access_token=user.fb_token, upload_phase='transfer',
                    start_offset=int(kwargs.get('start_offset')), upload_session_id=int(kwargs.get('upload_session_id')),
                    )


        # optional destination
        url = '%s%s/videos' % (self.api_video, user.fb_user_id)

        # Headers
        headers = {'Content-type': 'multipart/form-data'}

        req = requests.post(url=url, files=video_file, data=data, headers=headers)

Response from the server
{'error': {'code': 100, 'message': "Unsupported post request. Object with ID '1151655156896' does not exist, cannot be loaded due to missing permissions, or does not support this o
peration. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api", 'type': 'GraphMethodException', 'fbtrace_id': 'A1KZqqvi5pI', 'error_subcode': 33
}}

Application permissions:
user_videos,publish_actions
The user has been assigned developer status from the application.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-12-04
@maximkv25

from requests_toolbelt.multipart.encoder import MultipartEncoder

m = MultipartEncoder(
            fields=dict(
                access_token=user.fb_token,
                upload_phase='transfer',
                start_offset=kwargs.get('start_offset'),
                upload_session_id=kwargs.get('upload_session_id'),
                video_file_chunk=('chunk1.mp4', open(video_file_chunk, 'rb'),
                                  'multipart-form/data'),
            )
        )

        url = '%s%s/videos' % (self.api_video, user.fb_user_id)

        req = requests.post(url=url, data=m, headers={'Content-Type': m.content_type})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question