与flickr rest api、oauth和json响应接口的python库

python-flickr的Python项目详细描述


python flickr是一个与Flickr REST API&oauth接口的python库

功能

  • 照片上传
  • 检索用户信息
  • 常用的flickr方法
    • 添加/编辑/删除注释
    • 添加/编辑/删除注释
    • 还有更多(非常动态的库)!!
  • 所有的回答都以优美的口述返回

安装

安装python flickr很简单:

$ pip install python-flickr

用法

授权url

f = FlickrAPI(api_key='*your app key*',
          api_secret='*your app secret*',
          callback_url='http://www.example.com/callback/')

auth_props = f.get_authentication_tokens()
auth_url = auth_props['auth_url']

#Store this token in a session or something for later use in the next step.
oauth_token = auth_props['oauth_token']
oauth_token_secret = auth_props['oauth_token_secret']

print 'Connect with Flickr via: %s' % auth_url

单击“允许”后,请确保设置了一个url来处理定稿的令牌,并可能在以后将它们添加到数据库中以使用它们的信息。

处理回调

# 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 :('

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么运行按钮在Eclipse中不起作用?   java构造函数:实例变量是否为对象存储单独的值?   java使用jquery获取值   生产文件夹和源文件夹中的java单元测试   java递归问题这个解决方案正确吗?有更简单的解决方案吗?   java Android sqlite正在检索已选中中的行   javacom。谷歌。云数据存储。DatastoreException:请求缺少必需的身份验证凭据   java无法在Linux Ubuntu 12.04上运行maven2   在监视器的上下文中,被阻塞的线程在被notify()调用唤醒后在何处继续执行?   安卓如何从游标获取Bool(Java SQLite)   如何阻止rxjava runnable发出?   使用设备管理时应用程序中的java Android实例化异常