python(oauth2.0)

#Author:Mini#!/usr/bin/env pythonimport urllib.requestimport urllib.parsefrom sina.miniweibo import APIClientdef 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_tokenclient,access_token= weibo()#調用接口:(/轉爲__)客戶對象,方法,接口名,接口目錄,接口名(參數)rst=client.get.statuses__repost_timeline(id="FsMuBAG3n")print(rst)rst1=client.get.statuses__user_timeline(source="2911889655")print(rst1)
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息