欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python調用新浪微博API項目實踐

系統 1609 0

因為最近接觸到調用新浪微博開放接口的項目,所以就想試試用python調用微博API。

SDK下載地址:http://open.weibo.com/wiki/SDK 代碼不多十幾K,完全可以看懂。

有微博賬號可以新建一個APP,然后就可以得到app key和app secret,這個是APP獲得OAuth2.0授權所必須的。

了解OAuth2可以查看鏈接新浪微博的說明。 OAuth2授權參數除了需要app key和app secret還需要網站回調地址redirect_uri,并且這個回調地址不允許是局域網的(神馬localhost,127.0.0.1好像都不行),這個著實讓我著急了半天。我使用API也不是網站調用,于是查了很多。看到有人寫可以用這個地址替代,https://api.weibo.com/oauth2/default.html,我試了一下果然可以,對于?潘坷此凳歉齪孟?息。

下面先來個簡單的程序,感受一下:

設置好以下參數

            
import sys
import weibo
import webbrowser

APP_KEY = ''
MY_APP_SECRET = ''
REDIRECT_URL = 'https://api.weibo.com/oauth2/default.html'
          

獲得微博授權URL,如第2行,用默認瀏覽器打開后會要求登陸微博,用需要授權的賬號登陸,如下圖

            
api = weibo.APIClient(app_key=APP_KEY,app_secret=MY_APP_SECRET,redirect_uri=REDIRECT_URL)
authorize_url = api.get_authorize_url()
print(authorize_url)
webbrowser.open_new(authorize_url)

          

python調用新浪微博API項目實踐_第1張圖片

登陸后會調轉到一個連接https://api.weibo.com/oauth2/default.html?code=92cc6accecfb5b2176adf58f4c

關鍵就是code值,這個是認證的關鍵。手動輸入code值模擬認證

            
request = api.request_access_token(code, REDIRECT_URL)
access_token = request.access_token
expires_in = request.expires_in
api.set_access_token(access_token, expires_in)
api.statuses.update.post(status=u'Test OAuth 2.0 Send a Weibo!')
          

access_token就是獲得的token,expires_in是授權的過期時間 (UNIX時間)

用set_access_token保存授權。往下就可以調用微博接口了。測試發了一條微博

python調用新浪微博API項目實踐_第2張圖片

但是這樣的手動輸入code方式,不適合程序的調用,是否可以不用打開鏈接的方式來請求登陸獲取授權,經多方查找和參考,將程序改進如下,可以實現自動獲取code并保存,方便程序服務調用。

            
accessWeibo

# -*- coding: utf-8 -*- 
#/usr/bin/env python 

#access to SinaWeibo By sinaweibopy 
#實現微博自動登錄,token自動生成,保存及更新 
#適合于后端服務調用 


from weibo import APIClient 
import pymongo 
import sys, os, urllib, urllib2 
from http_helper import * 
from retry import * 
try: 
import json 
except ImportError: 
import simplejson as json 

# setting sys encoding to utf-8 
default_encoding = 'utf-8' 
if sys.getdefaultencoding() != default_encoding: 
reload(sys) 
sys.setdefaultencoding(default_encoding) 

# weibo api訪問配置 
APP_KEY = '' # app key 
APP_SECRET = '' # app secret 
REDIRECT_URL = 'https://api.weibo.com/oauth2/default.html' # callback url 授權回調頁,與OAuth2.0 授權設置的一致 
USERID = '' # 登陸的微博用戶名,必須是OAuth2.0 設置的測試賬號 
USERPASSWD = '' # 用戶密碼 


client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=REDIRECT_URL) 

def make_access_token(): 
#請求access token 
params = urllib.urlencode({
'action':'submit',
'withOfficalFlag':'0',
'ticket':'',
'isLoginSina':'', 
'response_type':'code',
'regCallback':'',
'redirect_uri':REDIRECT_URL,
'client_id':APP_KEY,
'state':'',
'from':'',
'userId':USERID,
'passwd':USERPASSWD,
}) 

login_url = 'https://api.weibo.com/oauth2/authorize' 

url = client.get_authorize_url() 
content = urllib2.urlopen(url) 
if content: 
headers = { 'Referer' : url } 
request = urllib2.Request(login_url, params, headers) 
opener = get_opener(False) 
urllib2.install_opener(opener) 
try: 
f = opener.open(request) 
return_redirect_uri = f.url 
except urllib2.HTTPError, e: 
return_redirect_uri = e.geturl() 
# 取到返回的code 
code = return_redirect_uri.split('=')[1] 
#得到token 
token = client.request_access_token(code,REDIRECT_URL) 
save_access_token(token) 

def save_access_token(token): 
#將access token保存到MongoDB數據庫
mongoCon=pymongo.Connection(host="127.0.0.1",port=27017)
db= mongoCon.weibo

t={
"access_token":token['access_token'],
"expires_in":str(token['expires_in']),
"date":time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
}
db.token.insert(t,safe=True) 

#Decorator 目的是當調用make_access_token()后再執行一次apply_access_token()
@retry(1) 
def apply_access_token(): 
#從MongoDB讀取及設置access token 
try: 

mongoCon=pymongo.Connection(host="127.0.0.1",port=27017)
db= mongoCon.weibo
if db.token.count()>0:
tokenInfos=db.token.find().sort([("_id",pymongo.DESCENDING)]).limit(1)
else: 
make_access_token() 
return False 

for tokenInfo in tokenInfos:
access_token=tokenInfo["access_token"]
expires_in=tokenInfo["expires_in"]

try: 
client.set_access_token(access_token, expires_in) 
except StandardError, e: 
if hasattr(e, 'error'): 
if e.error == 'expired_token': 
# token過期重新生成 
make_access_token()
return False 
else: 
pass 
except: 
make_access_token()
return False 

return True 

if __name__ == "__main__": 
apply_access_token() 

# 以下為訪問微博api的應用邏輯 
# 以發布文字微博接口為例
client.statuses.update.post(status='Test OAuth 2.0 Send a Weibo!')
          

            
retry.py

import math
import time

# Retry decorator with exponential backoff
def retry(tries, delay=1, backoff=2):
"""Retries a function or method until it returns True.

delay sets the initial delay, and backoff sets how much the delay should
lengthen after each failure. backoff must be greater than 1, or else it
isn't really a backoff. tries must be at least 0, and delay greater than
0."""

if backoff <= 1:
raise ValueError("backoff must be greater than 1")

tries = math.floor(tries)
if tries < 0:
raise ValueError("tries must be 0 or greater")

if delay <= 0:
raise ValueError("delay must be greater than 0")

def deco_retry(f):
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay # make mutable

rv = f(*args, **kwargs) # first attempt
while mtries > 0:
if rv == True or type(rv) == str: # Done on success ..
return rv

mtries -= 1 # consume an attempt
time.sleep(mdelay) # wait...
mdelay *= backoff # make future wait longer

rv = f(*args, **kwargs) # Try again

return False # Ran out of tries :-(

return f_retry # true decorator -> decorated function
return deco_retry # @retry(arg[, ...]) -> true decorator
          

            
http_helper.py

# -*- coding: utf-8 -*-
#/usr/bin/env python

import urllib2,cookielib

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_301(cls, req, fp, code, msg, headers):
result = urllib2.HTTPRedirectHandler.http_error_301(cls, req, fp, code, msg, headers)
result.status = code
print headers
return result

def http_error_302(cls, req, fp, code, msg, headers):
result = urllib2.HTTPRedirectHandler.http_error_302(cls, req, fp, code, msg, headers)
result.status = code
print headers
return result

def get_cookie():
cookies = cookielib.CookieJar()
return urllib2.HTTPCookieProcessor(cookies)

def get_opener(proxy=False):
rv=urllib2.build_opener(get_cookie(), SmartRedirectHandler())
rv.addheaders = [('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)')]
return rv
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美日韩综合一区 | 免费成人午夜视频 | 538prom国产在线视频一区 | 一级黄色片毛片 | 国产农村妇女毛片精品久久麻豆 | 久久久亚洲一区 | 成人不卡在线 | 国产睡熟迷奷系列网站 | 三级网址在线播放 | 国产欧美性综合视频性刺激 | 色午夜日本 | 国产精品视频一区二区三区 | 亚洲一区精品在线 | 午夜社区| 日韩欧美一区二区三区 | 狠狠躁夜夜躁人人爽视频 | 中文字幕三区 | 国产一区二区三区免费观看 | 日本免费观看网站 | 色噜噜狠狠狠狠色综合久 | 色爱av | 久久综合综合久久 | 国产成人免费视频网站高清观看视频 | 国产三级在线观看 | www97影院| 国产成人久久婷婷精品流白浆 | 欧美午夜影院 | 日日骚 | 三级毛片黄色 | 亚洲精品国偷拍自产在线观看 | 色哟哟哟在线观看www | 激情男女视频 | 国产日| 日韩欧美第一页 | 国产乱码精品一区二区三区中 | 国产精品久久久久无码AV1 | 亚洲www啪成人一区二区 | 精品在线播放 | 国产午夜亚洲精品国产 | 92精品国产自产在线观看48页 | 亚洲一区二区三区首页 |