如何通过POST到URL在不使用Facebook SDK的情况下发布照片?
在Python的Facebook开发工具包中,我们发现缺少上传照片的功能。其实,我想在不使用现有开发工具包的情况下上传照片。
那么,我该如何在Python中实现这些步骤呢?
You can publish a photo to a specific, existing photo album with a POST to http://graph.facebook.com/ALBUM_ID/photos.
curl -F 'access_token=...' \
-F 'source=@file.png' \
-F 'message=Caption for the photo' \
https://graph.facebook.com/me/photos
2 个回答
2
你可以使用urllib2或者poster模块来发送POST请求,看看下面这些问题:
2
这里不是一个便宜做事的地方,而是学习编程的地方,所以如果有人没有现成的代码,可能不会帮你完成工作。
不过,有几个建议:给你的浏览器装一个Tamper Data或者类似的插件,记录下你手动操作时发送的所有数据。之后,你只需要用Python的urllib库来模仿这些操作,并解析出你需要的非静态部分的HTML文档。
我对Facebook不太了解(没有账号),但你可能需要先登录并保持cookie,所以这里有一个我之前做过的类似的小例子。首先使用登录页面进行登录,然后再去我感兴趣的网站获取数据。
def get_site_content():
def get_response(response):
content = response.info()['Content-Type']
charset = content[content.rfind('=') + 1:] # ugly hack, check orderly
return response.read().decode(charset)
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
data = urllib.parse.urlencode({ "log" : "1", "j_username" : USERNAME, "j_password" : PASSWORD })
opener.open("url", data)
data = urllib.parse.urlencode({ "months" : "0" })
resp = opener.open("url2", data)
return get_response(resp)