2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Imgur APIを使って画像を匿名アップロードする(Python使用)

Last updated at Posted at 2019-12-07

ユーザ登録する

アプリケーションを作成

https://api.imgur.com/oauth2/addclient
スクリーンショット 2019-12-08 0.16.24.png

  • Application name: アプリケーション名
  • Application name: 匿名アップロードだけできれば良い場合はAnonymous usage without user authorizationを選択
  • Authorization callback URL: 匿名アップロードの場合は関係ない。適当なURLを入力しておく。
  • Application website (optional): 省略可
  • Email: 適当なメールアドレス
  • Description: 省略可

SubmitするとClient IDClient secretが払い出される。が、匿名アップロードではClient IDさえあれば良い。

投稿

ローカルにある画像をアップロード

client_id = 'YOUR CLIENT ID'
image_path = 'path/to/your/image'

import requests
headers = {
    'authorization': f'Client-ID {client_id}',
}
files = {
    'image': (open(image_path, 'rb')),
}

r = requests.post('https://api.imgur.com/3/upload', headers=headers, files=files)

import json
print(json.loads(r.text)['data']['link'])

既にWEB上にある画像をアップロード

image_url = 'YOUR IMAGE URL'
client_id = 'YOUR CLIENT ID'

import requests
headers = {
    'authorization': f'Client-ID {client_id}',
}
files = {
    'image' : requests.get(image_url).content
    }
r = requests.post('https://api.imgur.com/3/upload', headers=headers, files=files)

import json
print(json.loads(r.text)['data']['link'])
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?