Python-302接口重定向處理(跟隨重定向)


import requests

class OpenAdmin_Auth_Ticket(object):
    def get_cookie(self,url):
        res = requests.get(url).cookies
        return {"Cookie":"SESSION=%s" %res["SESSION"]}

    def get_login(self,url, params, headers):
        res = requests.post(url, data=params, headers=headers, allow_redirects=False) #敲重點 跟隨重定向 allow_redirects=False
        return res.headers["Location"]

    def get_openadmin_auth(self,url):
        res = requests.get(url, allow_redirects=False) #敲重點 跟隨重定向 allow_redirects=False
        JSESSIONID = res.cookies["JSESSIONID"]
        MAS_AUTH_TICKET = res.cookies["MAS_AUTH_TICKET"]
        cookie = {"Cookie": "JSESSIONID=%s;MAS_AUTH_TICKET=%s" % (JSESSIONID, MAS_AUTH_TICKET)}
        return cookie

    def get_admin_get(self,url, headers):
        res = requests.get(url, headers=headers)
        return res.json()

if __name__ == "__main__":
    get_auth = OpenAdmin_Auth_Ticket()

    #獲取后續操作的授權JSESSIONID和MAS_AUTH_TICKET
    iflow_url = "重定向URL"
    params = {
        "username": "test1",
        "password": "a123456"
    }

    location = get_auth.get_openadmin_auth(get_auth.get_login(iflow_url,params,get_auth.get_cookie(iflow_url)))

    # 獲取登錄用戶信息接口
    url = "獲取用戶信息的URL"
    print(get_auth.get_admin_get(url,location))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM