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使用Matplotlib繪圖及設(shè)置實(shí)例(用python制圖)
Python matplotlib包可以畫各種類型的圖,功能非常齊全,下面這篇文章主要給大家介紹了關(guān)于python使用Matplotlib繪圖及設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
python抓取豆瓣圖片并自動(dòng)保存示例學(xué)習(xí)
python抓取豆瓣圖片并自動(dòng)保存示例學(xué)習(xí),示例使用了beautifulsoup庫分析HTML代碼,beautifulsoup是一個(gè)HTML/XML解析器,可以用來做網(wǎng)頁爬蟲2014-01-01
Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫的方法示例
這篇文章主要介紹了Django結(jié)合使用Scrapy爬取數(shù)據(jù)入庫的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
教你用python編寫腳本實(shí)現(xiàn)自動(dòng)簽到
這篇文章主要介紹了教你怎樣用python編寫腳本實(shí)現(xiàn)自動(dòng)簽到,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Tensorflow安裝問題: Could not find a version that satisfies the
這篇文章主要介紹了Tensorflow安裝問題: Could not find a version that satisfies the requirement tensorflow,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

