gpt4 book ai didi

python - 如何使用 Python 3 将照片上传到 Facebook

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:58 25 4
gpt4 key购买 nike

好吧,我已经尝试了所有可能在互联网和论坛上找到的方法,但没有任何效果。许多模块,如 facebook-sdk 等都有语法错误,因为它们是为 python 2.X 构建的,而我在 python 3 上(我打算保持这种状态)。到目前为止,我最好的选择是使用 fbconsole。我可以轻松发布简单的状态更新等,但我似乎无法上传照片!?与此相关的一个问题是:我需要在 fbconsole.OAUTH_SCOPE = [] 中输入什么来允许我验证整个照片上传爵士乐?我的 urllib、urllib2 和 urllib.request.urlretrieve() 东西也都抛出错误...

如果有人能帮我解决这个问题,我很乐意为它制作视频或文字教程。如果我早点弄清楚,我也会这样做。祝你好运!

这是我的错误之一:

C:\Users\Zach\Downloads\facebook-sdk-master\facebook-sdk-master>python
Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import fbconsole
>>> fbconsole.OAUTH_SCOPE = ['publish_stream', 'read_stream']
>>> fbconsole.authenticate()
127.0.0.1 - - [30/Dec/2013 20:36:54] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [30/Dec/2013 20:36:54] "GET /?access_token=CAACjeiZB6FgIBAGkN1XsOxC4gtJ2ukeiZBnJr9sViG
QZBsV63yJFmVohvhz5meYUbMJAXWy4ZBNNsYcXoD4BgMqTjSuPP8n5U0uN8exqSZAOlUeMoYtDWElAZCQy9WMiDgNcRSVKz94oZC
qwh4uwaXPmZBuZBrgeENHCtkD5ZCWPvph9CIfZBpI9NcG7WSFeZBEdGeYZD&expires_in=4987 HTTP/1.1" 200 -
>>>
>>> fbconsole.post('/me/photos', {'message':'Test Picture', 'source':open('screenshot.jpg')})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 763, in post
File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 673, in post
File "C:\Python33\lib\urllib\request.py", line 471, in open
req = meth(req)
File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 142, in http_reque
st
File "C:\Python33\lib\site-packages\fbconsole-0.3-py3.3.egg\fbconsole.py", line 168, in multipart_
encode
File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 111: character maps to <undef
ined>
>>>

最佳答案

我猜你的代码是:

file = open('image.jpg', 'r')

让我们改成这样:

file = open('image.jpg', 'rb')

您可能会从 facebook python sdk 中获取错误。类似于 TypeError: sequence item 0: expected str instance, bytes found

我修复了几个错误并上传到这里:

https://github.com/tuanchauict/facebook-sdk-python3

你可以试试。

演示:

token = 'get_your_user_token_or_from_Graph_API_Explorer'
graph = fb.GraphAPI(token)

file = open('test.jpg', 'rb')

graph.put_photo(file, "this is a test photo")

关于python - 如何使用 Python 3 将照片上传到 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20850818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com