通过python将文件上传到imgur [英] uploading a file to imgur via python

查看:184
本文介绍了通过python将文件上传到imgur的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用python请求模块和Imgur API将图像上传到Imgur.

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

我的代码如下:

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'
    }
)

我通常会收到400响应错误.我不确定myu client_id是否错误,或者我的请求是否错误(我对url请求的经验很少),或者我使用的Imgur API错误.

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.

提交后,我还想获取图片的网址.我不确定API是否为此提供了命令,或者python.requests模块是否具有使我能够获取刚刚发布的数据的技巧.

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

但是,当我使用我的client_id并插入代码中使用的应用程序ID时,它也会返回400错误,以及我进行更改的时间

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

来自: url =" http://api.imgur.com/2/upload.json " 到: 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"

推荐答案

这是v3请求,但您没有使用SSL(这是强制性的).尝试设置

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

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

这篇关于通过python将文件上传到imgur的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆