python使用xauth方式登录饭否网然后发消息
时间:2021-04-18 08:11:54
开发环境: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()
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django 发送手机验证码的示例代码
![](https://img.aspxhome.com/file/2023/7/120377_0s.png)
利用python画一颗心的方法示例
![](https://img.aspxhome.com/file/2023/7/133257_0s.png)
python基于socketserver实现并发,验证客户端的合法性
一篇文章彻底弄懂Python字符编码
MySQL查询优化
Python实现的概率分布运算操作示例
Python机器学习利用鸢尾花数据绘制ROC和AUC曲线
![](https://img.aspxhome.com/file/2023/8/123648_0s.png)
python使用pymongo操作mongo的完整步骤
python requests抓取one推送文字和图片代码实例
改善IE6中a与a:hover的背景支持
mysql 获取表有多少列
PHP设计模式之观察者模式浅析
Python用HBuilder创建交流社区APP
Python中的二叉树查找算法模块使用指南
Python中输入若干整数以逗号间隔实现统计每个整数出现次数
![](https://img.aspxhome.com/file/2023/3/110003_0s.png)
Python图像处理库PIL的ImageEnhance模块使用介绍
![](https://img.aspxhome.com/file/2023/0/107650_0s.jpg)
剖析SQL Server 事务日志的收缩和截断
Python进阶多线程爬取网页项目实战
![](https://img.aspxhome.com/file/2023/1/123631_0s.jpg)
通过python模糊匹配算法对两个excel表格内容归类
![](https://img.aspxhome.com/file/2023/5/127595_0s.jpg)