国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python使用xauth方式登錄飯否網然后發消息

2020-02-23 05:17:31
字體:
來源:轉載
供稿:網友

開發環境: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',

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秦安县| 竹溪县| 长葛市| 淳安县| 阿瓦提县| 北碚区| 武清区| 漳浦县| 察雅县| 垦利县| 湖南省| 商洛市| 隆安县| 固镇县| 盐池县| 乌兰察布市| 睢宁县| 和静县| 宜丰县| 普格县| 义马市| 清原| 金湖县| 安徽省| 惠来县| 收藏| 兴仁县| 句容市| 宁陕县| 民乐县| 鹰潭市| 台湾省| 巧家县| 莆田市| 安龙县| 尚义县| 乐安县| 遂川县| 崇明县| 鄂伦春自治旗| 水城县|