user2589924
user2589924

Reputation: 11

Uploading photos or videos to Facebook using Python

I tried to upload photos or videos to facebook page using Python but I got HTTPError: HTTP Error 400: Bad Request. But it was Ok when I used form instead. let me show you the codes.

This is code for form.

<!DOCTYPE html>
<html>
<body>

<form enctype="multipart/form-data"  action="https://graph-video.facebook.com/videos/PAGE_ID/photos?access_token=ACCESS_TOKEN"  
 method="POST">
<input name="file" type="file">
<input type="submit" value="Upload" />
</form>

</body>
</html>

and This is my Python code.

video = open(args[0])

url = 'https://graph-video.facebook.com/videos/PAGE_ID'
    data = {'access_token': 'ACCESS_TOKEN',
            'title': 'test',
            'description': 'test',
            'source' : video
           }
    data1 = urllib.urlencode(data) 
    req = urllib2.Request(url, data1)
    r = urllib2.urlopen(req)

I think that access_token is not the issue since it worked when I used form.

please let me know how to upload videos or photos by Python. thanks.

Upvotes: 1

Views: 4874

Answers (1)

user1234567
user1234567

Reputation: 198

This works for me.

 import requests
 url='https://graph-video.facebook.com/100000198728296/videos?access_token='+str(access)
 path="/home/abc.mp4"
 files={'file':open(path,'rb')}
 flag=requests.post(url, files=files).text
 print flag

flag will return json containing video id on successful upload of video

Upvotes: 5

Related Questions