简体   繁体   English

通过python将文件上传到imgur

[英]uploading a file to imgur via python

I'm having trouble uploading an image to Imgur using the python requests module and the Imgur API. 我使用python请求模块和Imgur API将图像上传到Imgur时遇到问题。

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. 我通常会得到400响应错误。 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. 我不确定myu client_id是否错误,或者我的请求是否错误(我对url请求的经验很少),或者我是否使用了Imgur API错误。

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). 我不确定API是否有命令,或者如果python.requests模块有一个技巧可以让我获取刚刚发布的数据(POST)。

A very similar question was answered here, and the code actually worked!: Trouble sending a file to Imgur 这里回答了一个非常相似的问题,代码确实有效!: 麻烦发送文件到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 但是,当我使用我的client_id时,在代码中使用了应用程序ID,它返回了400错误,以及当我更改时

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

This is a v3 request, but you're not using SSL, which is mandatory. 这是一个v3请求,但您没有使用SSL,这是必需的。 Try setting 尝试设置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM