def login(): """Login to flickr with read only access.After successful login redirects to callback url else redirected to index page """ try: auth = AuthHandler(key=app.config['FLICKR_KEY'], secret=app.config['FLICKR_SECRET'], callback=url_for('flickr_callback', _external=True)) return redirect(auth.get_authorization_url('read')) except FlickrError, f: # Flash failed login & redirect to index page flash(u'Failed to authenticate user with flickr', 'error') return redirect(url_for('index'))
def protected_view(*args, **kwargs): if session.token: token = session.token #log.info('Getting token from session: %s' % token) return view(*args, **kwargs) else: # No valid token, so redirect to Flickr #log.info('Redirecting user to Flickr to get frob') #url = f.web_login_url(perms='read') a = AuthHandler(callback=CALLBACK_URL) url = a.get_authorization_url('read') session.a = a return redirect(url)
def test_bytes_response(self): from flickr_api import set_auth_handler auth_handler = AuthHandler(key='test', secret='test', access_token_key='test', access_token_secret='test') set_auth_handler(auth_handler) args = dict(photo_file='/tmp/test_file', photo_file_data=StringIO('000000')) module = inspect.getmodule(method_call) resp = Response() resp.status_code = 200 resp.raw = BytesIO(b'[0,1]') module.requests.post = MagicMock(return_value=resp) payload = module.requests.post.return_value.json() self.assertEqual(type(payload), list)
def test_upload_not_200(self): from flickr_api import set_auth_handler auth_handler = AuthHandler(key="test", secret="test", access_token_key="test", access_token_secret="test") set_auth_handler(auth_handler) args = dict(photo_file='/tmp/test_file', photo_file_data=StringIO("000000")) module = inspect.getmodule(upload) resp = Response() resp.status_code = 404 resp.raw = BytesIO("Not Found".encode("utf-8")) module.requests.post = MagicMock(return_value=resp) with self.assertRaises(FlickrError) as context: upload(**args) print(context.exception) self.assertEquals("HTTP Error 404: Not Found", str(context.exception))
def test_call_5XX(self): from flickr_api import set_auth_handler auth_handler = AuthHandler(key="test", secret="test", access_token_key="test", access_token_secret="test") set_auth_handler(auth_handler) args = dict(photo_file='/tmp/test_file', photo_file_data=StringIO("000000")) module = inspect.getmodule(method_call) resp = Response() resp.status_code = 502 resp.raw = BytesIO("Bad Gateway".encode("utf-8")) module.requests.post = MagicMock(return_value=resp) with self.assertRaises(FlickrServerError) as context: f.Person.findByUserName("tomquirkphoto") print(context.exception) self.assertEquals("HTTP Server Error 502: Bad Gateway", str(context.exception))
def init(key, secret): """ Initialize API. @see: http://www.flickr.com/services/api/ @param key: str, API key @param secret: str, API secret """ # TODO: save keys in file too, and share with download tool logging.debug('Initializing Flickr API ({0}/{1})'.format(key, secret)) Flickr.set_keys(key, secret) auth_file = os.path.expanduser(AUTH_FILE) logging.debug('Loading credentials from: {0}'.format(auth_file)) try: handler = AuthHandler.load(auth_file) set_auth_handler(handler) return except IOError: logging.warning('Could not load credentials from: {0}'.format(auth_file)) # fall through and create the file pass # Get new crendentials logging.debug('Retrieving new credentials from Flickr') handler = AuthHandler(key=key, secret=secret) print('Please authorize: {0}'.format(handler.get_authorization_url('write'))) # TODO: make landing page that extracts the verifier verifier = raw_input('Verifier: ') handler.set_verifier(verifier) try: handler.save(auth_file) except IOError: logging.warning('Could not save credentials to: {0}'.format(auth_file)) set_auth_handler(handler)