python使用xauth方式登錄飯否網(wǎng)然后發(fā)消息
開發(fā)環(huán)境: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()
相關(guān)文章
Python常用數(shù)據(jù)類型之間的轉(zhuǎn)換總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Python中常用數(shù)據(jù)類型之間的轉(zhuǎn)換相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下2019-09-09Python 給屏幕打印信息加上顏色的實(shí)現(xiàn)方法
這篇文章主要介紹了Python 給屏幕打印信息加上顏色的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04在django中實(shí)現(xiàn)choices字段獲取對(duì)應(yīng)字段值
這篇文章主要介紹了在django中實(shí)現(xiàn)choices字段獲取對(duì)應(yīng)字段值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-07-07Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能,結(jié)合實(shí)例形式分析了Python基于wmi模塊的遠(yuǎn)程連接與進(jìn)程操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06Python爬蟲爬取愛奇藝電影片庫(kù)首頁(yè)的實(shí)例代碼
這篇文章主要介紹了Python爬蟲爬取愛奇藝電影片庫(kù)首頁(yè)的實(shí)例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Python中數(shù)組,列表:冒號(hào)的靈活用法介紹(np數(shù)組,列表倒序)
下面小編就為大家分享一篇Python中數(shù)組,列表:冒號(hào)的靈活用法介紹(np數(shù)組,列表倒序),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-04-04python3第三方爬蟲庫(kù)BeautifulSoup4安裝教程
這篇文章主要為大家詳細(xì)介紹了python3第三方爬蟲庫(kù)BeautifulSoup4的安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的方法
這篇文章主要給大家介紹了關(guān)于利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python實(shí)現(xiàn)矩陣和array數(shù)組之間的轉(zhuǎn)換
今天小編就為大家分享一篇python實(shí)現(xiàn)矩陣和array數(shù)組之間的轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-11-11