2013-04-29 67 views
0

這是一個後續問題: uploading a file to imgur via python提交上傳Imgur圖片庫

我剛剛上傳的圖像Imgur,但我發現它不是公衆看法。在他們的API網站上,他們說如果你想讓它公開上傳,你應該把它提交給畫廊。

我編寫了一個小應用程序,可以用一個圖像做到這一點,但我不確定它是不是工作,因爲我沒有提出正確的要求,或者因爲我可能缺少使用Oauth2模塊,到目前爲止我沒有要求使用它(我只是上傳圖片,但我不需要查看其他用戶使用我的應用上傳的內容)。

import base64 
import json 
import requests 

from base64 import b64encode 

client_id = '6a06c6bb8360e78' 

headers = {"Authorization": "Client-ID 6a06c6bb8360e78"} 

api_key = '6a06c6bb8360e78' 

url = "https://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' 
    } 
) 


data=json.loads(j1.text)['data'] 
data_id=data['id'] 

data_id_str=data_id.encode('ascii','ignore') 

# Now I try to push the iamge to the gallery with id data: 

url_gallery = "https://api.imgur.com/3/gallery/image/{id}.json" 

r2 = requests.post(url_gallery, headers=headers, data={'title': 'First image', 'terms': '1', 'id': data_id_str}) 

答案後做,我得到: r2.text 是403錯誤:

u'{"data":{"error":"Unauthorized", 
"request":"\\/3\\/gallery\\/image\\/{id}.json", 
"parameters":"terms = 1, id = 1jhIc2R, title = First 
image","method":"POST"},"success":false,"status":403}' 

這是Imgur API鏈接,提交圖片,我不知道,我沒有錯:https://api.imgur.com/endpoints/gallery#to-gallery

回答

3

Alan來自Imgur here。您需要通過身份驗證才能提交到圖庫(因此未經授權的錯誤)。目前,您看起來像是匿名上傳,然後嘗試匿名提交到圖庫。您需要從OAuth2獲取圖庫部件的access_token。

+0

該死的,Imgur的艾倫;) – 2014-12-01 12:51:15