Want more features on Pastebin? Sign Up, it's FREE!
Guest

Imgur API3, working with OAuth2 with Python, v2

By: TankorSmash on Feb 24th, 2013  |  syntax: Python  |  size: 3.39 KB  |  views: 151  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import requests
  2. import pprint
  3.  
  4. #https://api.imgur.com/oauth2#auth_url
  5. def getPin(client_id, client_secret):
  6.     """build a URL for the user to navigate to and "Allow" the application"""
  7.  
  8.     resp = "pin"
  9.     #can be any string at all
  10.     state = "anything"
  11.     url = r"https://api.imgur.com/oauth2/authorize?client_id={cid}&response_type={resp}&state={app_state}"
  12.  
  13.     print "browse to the following URL and grab the pin:"
  14.     pin_url = url.format(cid=client_id,resp= resp, app_state=state)
  15.     print pin_url
  16.    
  17.     return pin_url
  18.  
  19.  
  20. def exchangePinForTokens(client_id, client_secret, pin):
  21.     """takes the client_id and client_secret from the registered application URL,
  22.    along with the pin returned from `getPin()`, and return an access_token and a
  23.    refresh_token"""
  24.  
  25.     #the query parameters you'll send along with the POST request
  26.     params ={ "client_id" :client_id,
  27.              "client_secret" : client_secret,
  28.              "grant_type" : "pin",
  29.              "pin": pin}
  30.  
  31.     url = r"https://api.imgur.com/oauth2/token/"
  32.  
  33.     #make sure the data is sent with the POST request, along with disabling the
  34.     # SSL verification, potential security warning
  35.     r = requests.post(url, data = params, verify=False)
  36.     j= r.json()
  37.     print "The exchangePinForTokens API response:"
  38.     pprint.pprint(j)
  39.  
  40.     #add the access_token to the headers as
  41.     # Authorization: Bearer YOUR_ACCESS_TOKEN
  42.     access_token= j['access_token']
  43.     refresh_token= j['refresh_token']
  44.     print "Access Token: {0}\nRefresh Token: {1}".format(access_token,
  45.             refresh_token)
  46.  
  47.     return (access_token, refresh_token)
  48.  
  49.  
  50. def uploadImage(access_token, image_url):
  51.     """uploads an image using it's URL, the access_token is required"""
  52.  
  53.     #need to include the authorization headers,
  54.     # in order to make use of the access token
  55.     headers = {"authorization":"Bearer {0}".format(access_token)}
  56.  
  57.     upload_url = r'https://api.imgur.com/3/upload'
  58.  
  59.     #this is the data we'll POST to the api's URL
  60.     payload = {'image' : image_url,
  61.                 'type':'url',
  62.                 'title':"WORKS"}
  63.  
  64.     #make the upload, ensuring that the data, headers are included, and
  65.     # make sure to disable the verification of the SSL. Potential insecurty though
  66.     r = requests.post(upload_url, data=payload, headers=headers, verify=False)
  67.  
  68.     #save the json response, print it to screen
  69.     j = r.json()
  70.     print "The UploadImage API response:"
  71.     pprint.pprint(j)
  72.    
  73.     #print the img URL to verify that the image is still  there
  74.     uploaded_url = j['data']['link']
  75.     print "The uploaded image URL is: {0}".format(uploaded_url)
  76.      
  77.    
  78.  
  79. #a popular Python idiom to make sure that the following code gets run when this
  80. # file is ran as __main__, rather than imported
  81. if __name__ == '__main__':
  82.     """Run the following if module is top module"""
  83.  
  84.     #found here: https://api.imgur.com/oauth2/addclient
  85.     client_id= r"client_id_from_addclient"
  86.     client_secret= r"client_secret_from_addclient"
  87.    
  88.     image_url = r'http://www.personal.psu.edu/afr3/blogs/siowfa12/success.jpeg'
  89.    
  90.     #URL needed to have the user visit and allow the application to use the pin
  91.     getPin(client_id,client_secret)
  92.     pin = raw_input("input PIN\n")
  93.    
  94.     access_token, refresh_token = exchangePinForTokens(client_id,client_secret,pin)
  95.     uploadImage(access_token, image_url)
clone this paste RAW Paste Data