与flickr rest api、oauth和json响应接口的python库
python-flickr的Python项目详细描述
python flickr是一个与Flickr REST API&oauth接口的python库
功能
- 照片上传
- 检索用户信息
- 常用的flickr方法
- 添加/编辑/删除注释
- 添加/编辑/删除注释
- 还有更多(非常动态的库)!!
- 所有的回答都以优美的口述返回
安装
安装python flickr很简单:
$ pip install python-flickr
用法
处理回调
# oauth_token and oauth_token_secret come from the previous step # if needed, store those in a session variable or something f = FlickrAPI(api_key='*your app key*', api_secret='*your app secret*', oauth_token=oauth_token, oauth_token_secret=oauth_token_secret) authorized_tokens = f.get_auth_tokens(oauth_verifier) final_oauth_token = authorized_tokens['oauth_token'] final_oauth_token_secret = authorized_tokens['oauth_token_secret'] # Save those tokens to the database for a later use?
获取用户最近的活动feed
# Get the final tokens from the database or wherever you have them stored f = FlickrAPI(api_key='*your app key*', api_secret='*your app secret*', oauth_token=final_tokens['oauth_token'], oauth_token_secret=final_tokens['oauth_token_secret']) recent_activity = f.get('flickr.activity.userComments') print recent_activity
在照片上添加评论
# Assume you are using the FlickrAPI instance from the previous section add_comment = f.post('flickr.photos.comments.addComment', params={'photo_id': '6620847285', 'comment_text': 'This is a test comment.'}) #This returns the comment id if successful. print add_comment
删除对照片的评论
# Assume you are using the FlickrAPI instance from the previous section # If the comment is already deleted, it will throw a FlickrAPIError (In this case, with code 2: Comment not found.) del_comment = f.post('flickr.photos.comments.deleteComment', params={'comment_id':'45887890-6620847285-72157628767110559'}) print del_comment
上传照片
# Assume you are using the FlickrAPI instance from the previous section files = open('/path/to/file/image.jpg', 'rb') add_photo = f.post(params={'title':'Test Title!'}, files=files) print add_photo # Returns the photo id of the newly added photo
捕捉错误
# Assume you are using the FlickrAPI instance from the previous section try: # This comment was already deleted del_comment = f.post('flickr.photos.comments.deleteComment', params={'comment_id':'45887890-6620847285-72157628767110559'}) except FlickrAPIError, e: print e.msg print e.code print 'Something bad happened :('