I use this code from PyImgur's docs:
import pyimgur
CLIENT_ID = "xxx"
PATH = "image.jpg"
im = pyimgur.Imgur(CLIENT_ID)
uploaded_image = im.upload_image(PATH, title="Uploaded with PyImgur")
print(uploaded_image.title)
print(uploaded_image.link)
print(uploaded_image.size)
print(uploaded_image.type)
But it doesn't show up in my gallery
Problem is how all APIs work (not only imgur API
).
CLIENT_ID
gives access to API but only as anonymous user.
Program with the same CLIENT_ID
can be used by different users (not only by you) and every user has to allow to access to his/her data.
First program has to generate special URL to imgur.com
(I'm not sure if it need CLIENT_SECRET
for this)
im = pyimgur.Imgur(CLIENT_ID)#, client_secret=CLIENT_SECRET)
auth_url = im.authorization_url('pin')
Next you have to open this URL in web browser and it will ask you to login to imgur.com
and it may ask you if you allow this program to access to your data. If you allow then it will display page with unique number pin
which you have to use in program
access_token, refresh_token = im.exchange_pin(pin)
and then program will have access to your data and it can upload to your account - until you close program.
This also gives you access_token
(which is like login and password in one number) which program can use to access your data after restarting program - so you don't have to login again to imgur.com
and copy pin
.
For security reason access_token
expires after 60 minutes so you get also refresh_token
(which never expire) which program has to use every 60 minutes to generate new access_token
.
im = pyimgur.Imgur(CLIENT_ID, access_token=access_token, refresh_token=refresh_token)
Now it will upload image to your account after restarting program.
Minimal working code.
I use try/except
to load access_token
, refresh_token
from file or open web page for pin
to generate access_token
, refresh_token
. This method I saw in some example for Google API (but it was using pickle
to keep tokens in file).
import pyimgur
import webbrowser
CLIENT_ID = "123456789012345"
#CLIENT_SECRET = 'xxxyyyzzz'
# access to user data
try:
# try to loading access_token, refresh_token from file
with open('tokens.txt') as f:
access_token, refresh_token = f.read().strip().split()
# run with access_token, refresh_token
im = pyimgur.Imgur(CLIENT_ID, access_token=access_token, refresh_token=refresh_token)
except FileNotFoundError as ex:
# ask for new access_token, refresh_token when file doesn't exists
# run without access_token, refresh_token
im = pyimgur.Imgur(CLIENT_ID) #, client_secret=CLIENT_SECRET)
# generate url
auth_url = im.authorization_url('pin')
# open url in web browser
webbrowser.open(auth_url)
# ask for `pin` from web page
pin = input("What is the pin? ")
# get access_token, refresh_token
access_token, refresh_token = im.exchange_pin(pin)
# save access_token, refresh_token in file
with open('tokens.txt', 'w') as f:
f.write(f'{access_token} {refresh_token}')
# upload image
PATH = "lenna.png"
uploaded_image = im.upload_image(PATH, title="Uploaded with PyImgur")
print('[upload] title:', uploaded_image.title)
print('[upload] link :', uploaded_image.link)
print('[upload] size :', uploaded_image.size)
print('[upload] type :', uploaded_image.type)
# share with community - to see image (as post) on imgur's main page
uploaded_image.submit_to_gallery(title='Shared with PyImgur')
This add image to user account but I couldn't add to selected album. There is upload_image(..., album=...)
but it needs album's ID, not its name. Using im.search_gallery(query)
I could even get album's ID but I always get error that I can't change information in album which is in gallery.
EDIT:
This code adds image to your account and you can get its link and send this link to other people in mail, messanger or put link on some forum, or use it to display image on your own web page
... but this not send it on main page of Imgur - it doesn't "share to community"
.
You have to add
uploaded_image.submit_to_gallery(title='Title of your post')
And then people can see it on Imgur's main page and vote it.
I added this line to code above.
PyImgur source code: Image.submit_to_gallery(), Album.submit_to_gallery()
When you allow your program to access your data then you should see your program in Apps Used
and you can use revoke access
to disallow program to access your data
I use Client ID
for my application furas-imgur
so after allowing to access my data I see furas-imgur
in Apps Used