您的位置:首页 > 编程语言 > Python开发

使用Python编写客户端 上传文字or图片至新浪微博 by OAuth 2.0

2013-02-18 14:25 1126 查看
此链接为新浪微博推荐的Python SDK:https://github.com/michaelliao/sinaweibopy/wiki/OAuth2-HOWTO

Python SDK 介绍:http://michaelliao.github.com/sinaweibopy/

此链接为使用OAuth 1.0 实现的:http://blog.csdn.net/presidentpresident/article/details/7555979

以下是OAuth 2.0 实现代码:

# -*- coding:utf-8 -*- #
#! /usr/bin/env python

from weibo import *

def send_sina_weibo():

APP_KEY = '77734XXXX'
APP_SECRET = '5fa146e5e4a590523969c3de2e7xxxxx'

CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html'
client = APIClient(app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALLBACK_URL)
url = client.get_authorize_url()
##    code = your.web.framework.request.get('code')
print url
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)

r = client.request_access_token(raw_input("input code:").strip())
access_token = r.access_token
expires_in = r.expires_in
print access_token
print expires_in
client.set_access_token(access_token,expires_in)

print client.post.statuses__update(status=u'发送微博again by OAuth2.0')
print client.get.statuses__user_timeline()
print client.upload.statuses__upload(status=u'发送图片 by OAuth2.0', pic=open('test.gif'))

if __name__ == '__main__':
send_sina_weibo()
运行上述代码可能会抛出异常 “HTTPERROR 503”

只要将:

pic=open('test.gif') 改为 pic=open('test.gif', 'rb')
即可。

另外,本文参考了:http://blog.csdn.net/dyx1024/article/details/8470983
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐