uploading a file to imgur via python

不打扰是莪最后的温柔 提交于 2019-12-22 08:22:02

问题


I'm having trouble uploading an image to Imgur using the python requests module and the Imgur API.

My code is the following:

import base64
import json
import requests

from base64 import b64encode

client_id = 'my-client-id'

headers = {"Authorization": "Client-ID my-client-id"}

api_key = 'my-api-key'

url = "http://api.imgur.com/3/upload.json"

j1 = requests.post(
    url, 
    headers = headers,
    data = {
        'key': api_key, 
        'image': b64encode(open('1.jpg', 'rb').read()),
        'type': 'base64',
        'name': '1.jpg',
        'title': 'Picture no. 1'
    }
)

I usually get a 400 response error. I'm not sure if myu client_id is wrong, or if my request is wrong (I have very little experience on url requesting), or if I'm using the Imgur API wrong.

I'd also like to get the url of the image once I have submitted this. I'm not sure if the API has a command for that, or if the python.requests module has a trick that can let me GET the data I just posted(POST).

A very similar question was answered here, and the code actually worked!: Trouble sending a file to Imgur

However when I used my client_id, insted of the application ID that was used in the code, it returned a 400 error, as well as when I changed

from: url = "http://api.imgur.com/2/upload.json" to: url = "http://api.imgur.com/3/upload.json"


回答1:


This is a v3 request, but you're not using SSL, which is mandatory. Try setting

url = "https://api.imgur.com/3/upload.json"
#          ^


来源:https://stackoverflow.com/questions/16244183/uploading-a-file-to-imgur-via-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!