2013-07-17 57 views
1

我試圖使用Python將照片或視頻上傳到Facebook頁面,但我得到了 HTTPError:HTTP Error 400:Bad Request。但是,當我使用form的時候就沒問題了。 讓我告訴你代碼。使用Python將照片或視頻上傳到Facebook

這是表單的代碼。

<!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> 

這是我的Python代碼。

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) 

我認爲access_token不是問題,因爲它在我使用form時起作用。

請讓我知道如何通過Python上傳視頻或照片。謝謝。

+0

我的猜測是,你的問題是從一個在你的第二個請求(Python)的缺失'enctype'部分未來。 –

回答

1

這對我有用。

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 

標誌將返回一個包含視頻ID在視頻的上傳成功JSON

相關問題