def HandleOauthRequestResponse(cookie): oauth_verifier = parsed['oauth_verifier'][0] import flickr_api as f f.disable_cache() a = f.auth.AuthHandler.fromdict({ 'api_key': keys.kAPI_KEY, 'api_secret': keys.kAPI_SECRET, 'request_token_key': cookie["request_token_key"].value, 'request_token_secret': cookie["request_token_secret"].value, }) try: a.set_verifier(oauth_verifier) f.set_auth_handler(a) except urllib2.HTTPError as e: return HandleNotLoggedIn(cookie) # Should be able to login now. user = utils.AutoRetry(f.test.login) # Add details to MySQL. token_id = database.AddToDatabase(user.id, a.access_token.key, a.access_token.secret) # Store userId in a cookie so we can attempt to avoid logging in next time. cookie["user_id"] = user.id cookie["token_id"] = token_id print cookie.output() # Can just display page now. HandlePage(user, token_id)
def HandleNotLoggedIn(cookie): import flickr_api as f f.disable_cache() authHandler = f.auth.AuthHandler(keys.kAPI_KEY, keys.kAPI_SECRET, callback=keys.kCALLBACK_URL) f.set_auth_handler(authHandler) url = authHandler.get_authorization_url("write") cookie["request_token_key"] = authHandler.request_token.key cookie["request_token_secret"] = authHandler.request_token.secret print cookie.output() print 'Location: %s\n' % (url)
def HandleResumeSession(cookie): token, secret = database.GetOAuthTokenAndSecret(cookie["user_id"].value, cookie["token_id"].value) if token == None or secret == None: return False import flickr_api as f f.disable_cache() a = f.auth.AuthHandler.fromdict({ 'api_key': keys.kAPI_KEY, 'api_secret': keys.kAPI_SECRET, 'access_token_key': token, 'access_token_secret': secret, }) try: f.set_auth_handler(a) HandlePage(f.test.login(), cookie["token_id"].value) return True except urllib2.HTTPError as e: pass return False
def enable_cache(enable): if enable: f.enable_cache() else: f.disable_cache()
def CreateReturnJson(success, message): return json.dumps( { "result" : ["error","success"][success], ["error_message","message"][success] : message } ) if __name__ == '__main__': print "Content-type: text/html;charset=utf-8\n" fullUrl = utils.GetFullUrl() parsed_path = urlparse.urlparse(fullUrl) parsed = urlparse.parse_qs(parsed_path.query) oauth_token, oauth_token_secret = database.GetOAuthTokenAndSecret(parsed['userId'][0], parsed['tokenId'][0]) import flickr_api as f f.disable_cache() f.set_auth_handler(f.auth.AuthHandler.fromdict({ 'api_key' : keys.kAPI_KEY, 'api_secret' : keys.kAPI_SECRET, 'access_token_key' : oauth_token, 'access_token_secret' : oauth_token_secret, })) user = utils.AutoRetry(f.test.login) photoSets = utils.AutoRetry(user.getPhotosets) photoSet = next((x for x in photoSets if x.id == parsed['albumId'][0]), None) if photoSet == None: print CreateReturnJson(False, "Invalid photoset ID") else: photos = utils.AutoRetry(photoSet.getPhotos, extras='date_taken') for i in range(photos.info.page + 1, photos.info.pages + 1):