与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第三方库


热门话题
swing Java按钮/网格布局   java列出Google日历中的所有事件   java无效:单击API publisher test按钮后连接到后端时出错   带有内部赋值的java While循环导致checkstyle错误   java为什么trimToSize/ensureCapacity方法提供“公共”级访问?   文件输出流的java问题   ListIterator和并发修改异常的java问题   java如何使用两个URL映射   无法识别使用“./../”构造的字符串java相对路径,为什么?   首次写入remotelyclosedsocket不会触发异常,对吗?JAVA   java OneDrive REST API为文件上载提供了400个无效谓词   Java泛型、集合接口和对象类的问题   OpenSSL Java安全提供程序   jmeter java运行jmx禁用操作