与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同时读取输入流   安卓中的java短信监听   java你能帮我弄清楚如何在我的测验程序中组织数据吗?   列表中的java对   Java中的异常重复   通过SSH | Netbeans进行java远程调试   JAVAutil。扫描程序Java扫描程序在循环后读取整数失败?   用于温度转换器的构造函数java基本访问器方法   java如何将地图封装到自定义对象中   java更改布局不透明度,Progressbar除外   在REST中将java对象转换为XML时出错   java Spring引导IllegalArgumentException:找不到类[org.hibernate.cfg.ImprovedNamingStrategy]]   java Spring REST API,响应中的自定义实体字段   java数据库将null返回给布尔变量   mysql如何在java中将“'”转换为“`”