一个后台服务的入口,需要通过python api访问新浪微博,实现了token存储及自动更新。现分享出来,有需要的可以拿去用,自己增加业务处理。
https://github.com/wtmmac/Sina-weibo-api-access-by-python
# -*- coding: utf-8 -*-
#/usr/bin/env python
__version__ = '1.0'
__author__ = 'http://weibo.com/wtmmac'
'''
Demo for sinaweibopy
主要实现token自动生成及更新
适合于后端服务相关应用
'''
# api from:http://michaelliao.github.com/sinaweibopy/
from weibo import APIClient
import sys, os, urllib, urllib2
from http_helper import *
from retry import *
try:
import json
except ImportError:
import simplejson as json
# setting sys encoding to utf-8
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
# weibo api访问配置
APP_KEY = '' # app key
APP_SECRET = '' # app secret
CALLBACK_URL = '' # callback url 授权回调页,与OAuth2.0 授权设置的一致
USERID = '' # 微博用户名
USERPASSWD = '' # 用户密码
# token file path
save_access_token_file = 'access_token.txt'
file_path = os.path.dirname(__file__) + os.path.sep
access_token_file_path = file_path + save_access_token_file
client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
def make_access_token():
'''请求access token'''
params = urllib.urlencode({'action':'submit','withOfficalFlag':'0','ticket':'','isLoginSina':'', \
'response_type':'code', \
'regCallback':'', \
'redirect_uri':CALLBACK_URL, \
'client_id':APP_KEY, \
'state':'', \
'from':'', \
'userId':USERID, \
'passwd':USERPASSWD, \
})
login_url = 'https://api.weibo.com/oauth2/authorize'
url = client.get_authorize_url()
content = urllib2.urlopen(url)
if content:
headers = { 'Referer' : url }
request = urllib2.Request(login_url, params, headers)
opener = get_opener(False)
urllib2.install_opener(opener)
try:
f = opener.open(request)
print f.headers.headers
return_callback_url = f.geturl
# print f.read()
except urllib2.HTTPError, e:
return_callback_url = e.geturl()
# 取到返回的code
code = return_callback_url.split('=')[1]
#得到token
token = client.request_access_token(code)
save_access_token(token)
def save_access_token(token):
'''将access token保存到本地'''
f = open(access_token_file_path, 'w')
f.write(token['access_token']+' ' + str(token['expires_in']))
f.close()
@retry(1)
def apply_access_token():
'''从本地读取及设置access token'''
try:
token = open(access_token_file_path, 'r').read().split()
if len(token) != 2:
make_access_token()
return False
# 过期验证
access_token, expires_in = token
try:
client.set_access_token(access_token, expires_in)
except StandardError, e:
if hasattr(e, 'error'):
if e.error == 'expired_token':
# token过期重新生成
make_access_token()
else:
pass
except:
make_access_token()
return False
if __name__ == "__main__":
apply_access_token()
# 以下为访问微博api的应用逻辑
# 以接口访问状态为例
status = client.get.account__rate_limit_status()
print json.dumps(status)
分享到:
相关推荐
使用Python开发的基于新浪微博API的迷你微博网站+源码,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 使用Python开发的基于新浪微博API的迷你微博网站+源码,...
一个模拟浏览器的新浪微博客户端,采用python实现,现在可以上传图片,希望对那些想做脚本自动发微博的朋友有所帮助。
新浪微博爬虫,用python爬取新浪微博数据
本程序可以连续爬取一个或多个新浪微博用户(如胡歌、迪丽热巴、郭碧婷)的数据,并将结果信息写入文件或数据库。写入信息几乎包括用户微博的所有数据,包括用户信息和微博信息两大类。因为内容太多,这里不再赘述,...
新浪微博爬虫,用python爬取新浪微博数据,并下载微博图片和微博视频
用新浪微博SDK和python自动发送带图微博; 有详细步骤; 可能需要一下午的时间熟悉。
新浪微博爬虫,用python爬取新浪微博数据,并下载微博图片和微博视频 连续爬取一个或多个微博用户的数据,并将结果信息写入文件。写入信息几乎包括了用户微博的所有数据,主要有用户信息和微博信息两大类,前者包含...
这是新浪微博爬虫,采用python+selenium实现。 免费资源,希望对你有所帮助,虽然是傻瓜式爬虫,但是至少能运行。同时rar中包括源码及爬取的示例。 参考我的文章: ...[python爬虫] Selenium爬取新浪微博内容及用户...
多进程异步爬取新浪微博下的各项微博,目前已完成对账号下视频的爬取,以后会对图片,音乐等爬取,并完善程序
Scrapy爬取新浪微博用户信息、用户微博及其微博评论转发
爬取新浪微博信息:因为微博移动端的信息比PC端更容易爬取,所以本脚本是利用微博移动端爬取信息,爬虫微博访问自己的页面和访问其他用户的页面,得到的网页格式不同,所以无法爬取自己的微博信息