我想重現下面捲曲-F請求(from the Messenger API)發送圖像文件和參數Messenger的API發送:的Python:捲曲-F等效採用請求或其他
curl \
-F recipient='{"id":"USER_ID"}' \
-F message='{"attachment":{"type":"image", "payload":{}}}' \
-F [email protected]/tmp/shirt.png \
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
我使用的要求我嘗試了很多很多的事情(包括正確的收件人身份證,等等。例如:
recipient = '{"id":"USER_ID"}'
message = '{"attachment":{"type":"image", "payload":{}}}'
files = [("recipient", recipient), ("message", message), ("filedata", open(imagePath, 'rb'))]
r = requests.post('https://graph.facebook.com/v2.6/me/messages?access_token=ACCESS_TOKEN', files=files)
我也試過:
files = {"recipient" : recipient, "message" : message, "filedata" : open(imagePath, "rb")}
我幾乎總是從Messenger中收到此響應誤差:
'{"error":{"message":"(#100) param recipient must be non-empty.","type":"OAuthException","code":100,"fbtrace_id":"xxxxx"}}'
我不知所措就在這裏。我可能以某種方式濫用Requests語法,或者向API發送格式不正確的字符串,但是我已閱讀請求文檔並通過StackOverflow瀏覽此問題,並且無法解決我的問題。謝謝。
文件變量需要是一個字典,而不是一個元組數組。 – miah
如果您確信這是問題,您能否在答案中提供示例代碼?我已經嘗試過使用字典。語法是: files = {「recipient」:recipient,「message」:message,「filedata」:open(imagePath,「rb」)} – Konrad