python(oauth2.0)

#Author:Mini
#!/usr/bin/env python
import urllib.request
import urllib.parse
from sina.miniweibo import APIClient
def weibo():
APP_KEY="2911889655"
APP_SECRET="a87782ecfc82aa969f2f99ee285777bd"
CALL_BACK="https://api.weibo.com/oauth2/default.html"
AUTH_URL="https://api.weibo.com/oauth2/default.html"
USER_ID="5124480538"
PASSWD="mini111"
client=APIClient(app_key=APP_KEY,app_secret=APP_SECRET,redirect_uri=CALL_BACK)
refer_url= client.get_authorize_url()
print("refer url:"+refer_url)
cookies=urllib.request.HTTPCookieProcessor()
opener = urllib.request.build_opener(cookies)
urllib.request.install_opener(opener)
postdata={
"client_id":APP_KEY,
"redirect_uri":CALL_BACK,
"userId":USER_ID,
"password":PASSWD,
"IsLoginSina":"0",
"action":"submit",
"response_type":"code",
}
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
"Host":"api.weibo.com" ,
"Referer":refer_url
}
req=urllib.request.Request(
url=AUTH_URL,
data=urllib.parse.urlencode(postdata).encode("utf-8"),
headers=headers
)
try:
resp=urllib.request.urlopen(req)
print("callbackurl is %s" % resp.geturl())
pat = "code=(.*?)%"
print(resp.geturl())
code = input("input the retured code : ")
print("code is %s"% code)
except Exception as e:
print(e)
r=client.request_access_token(code)
access_token=r.access_token
expires_in=r.expires_in
print("access_token:",access_token)
print("expires_in:",expires_in)
client.set_access_token(access_token, expires_in)
return client,access_token
client,access_token= weibo()
#调用接口:(/转为__)客户对象,方法,接口名,接口目录,接口名(参数)
rst=client.get.statuses__repost_timeline(id="FsMuBAG3n")
print(rst)
rst1=client.get.statuses__user_timeline(source="2911889655")
print(rst1)

转载于:https://www.cnblogs.com/rabbittail/p/7755385.html

你可能感兴趣的:(python(oauth2.0))