python使用xauth方式登录饭否网然后发消息
2014-04-11 00:00
627 查看
开发环境:python版本2.X
#!/usr/bin/env python # -*- coding:utf-8 -*- # 适合python版本:2.X import sys, urllib, re import oauth.oauth as oauth from urllib2 import Request, urlopen status = 'hello world !' # send message consumer_key = '...' # api key consumer_secret = '...' # api secret access_token_url = 'http://fanfou.com/oauth/access_token' verify_url = 'http://api.fanfou.com/account/verify_credentials.xml' post_url = 'http://api.fanfou.com/statuses/update.xml' def request_to_header(request, realm=''): """Serialize as a header for an HTTPAuth request.""" auth_header = 'OAuth realm="%s"' % realm # Add the oauth parameters. if request.parameters: for k, v in request.parameters.iteritems(): if k.startswith('oauth_') or k.startswith('x_auth_'): auth_header += ', %s="%s"' % (k, oauth.escape(str(v))) return {'Authorization': auth_header} # get username and password from command line username = sys.argv[1] passwd = sys.argv[2] consumer = oauth.OAuthConsumer(consumer_key, consumer_secret) params = {} params["x_auth_username"] = username params["x_auth_password"] = passwd params["x_auth_mode"] = 'client_auth' request = oauth.OAuthRequest.from_consumer_and_token(consumer, http_url=access_token_url, parameters=params) signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1() request.sign_request(signature_method, consumer, None) headers=request_to_header(request) resp = urlopen(Request(access_token_url, headers=headers)) token = resp.read() print token # access_token got m = re.match(r'oauth_token=(?P<key>[^&]+)&oauth_token_secret=(?P<secret>[^&]+)', token) if m: oauth_token = oauth.OAuthToken(m.group('key'), m.group('secret')) params['status']=status request = oauth.OAuthRequest.from_consumer_and_token(consumer, http_method='POST', token=oauth_token, http_url=post_url, parameters=params) request.sign_request(signature_method, consumer, oauth_token) headers=request_to_header(request) resp = urlopen(Request(url=post_url, data=urllib.urlencode({'status':status}), headers=headers)) print resp.read()
相关文章推荐
- python使用xauth方式登录饭否网然后发消息
- 利用webqq协议使用python登录qq发消息源码参考
- 利用webqq协议使用python登录qq发消息
- 利用webqq协议使用python登录qq发消息源码参考
- 使用python来玩转ensp~系列1-telnet方式登录管理设备
- 不用python 微博sdk, 使用python裸体登录微博, 然后发一条微博, 再退出微博
- 使用python对登录密码爆破
- 表单form数据默认以键值对的形式将数据回发到服务器,回发非纯文本需使用enctype=multipart/form-data方式编码为整条消息
- Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法
- Python学习笔记(九) -- Python HttpClient使用requests库发送http消息
- 纯java代码使用ssh方式登录linux服务
- python中使用selenium模块登录QQ邮箱
- win7 caffe使用笔记——特征图可视化(matlab,python两种方式)
- 使用PuTTY登录Linux的两种验证方式
- python使用List和类的方式构建树和二叉树
- python-使用用户与密码登录验证的检索特定字段ver2(增加退出)
- Python使用mechanize模拟登录、抓取数据的代码
- 使用python+Selenium2+chrome模拟用户登录
- Python不使用函数,实现用户登录控制
- Linux中mysql忘记密码,或者登录不上,等等,都可使用以下方式