1. 程式人生 > >python獲取token和cookie方法

python獲取token和cookie方法

 獲取token:token為介面引數返回值

# -*- coding: utf-8 -*-
# @File    : get_token.py
# @API_name:
# @Time    : 2018/12/11 10:37
# @Author  : guozhen.deng

# 獲取token ,處理邏輯:
# 傳遞cookie 至http://a**********l.com/initiator/live/get-token,
# 傳遞引數:activityId:519903328

import requests
def get_token(token_url,token_data,token_headers):#在公共變數進行方法傳參,新增相應變數,否則不新增
    # 獲取token:呼叫get_token方法
    # token_url = "http://***********m/initiator/live/get-token"
    # token_data = {"activityId": "******"}
    # token_headers = {"Cookie": "vmpbId=*******1935d5323cda6"}

    t = requests.post(token_url,data=token_data,headers=token_headers)
    token = t.json()
    return token["data"]

if __name__ == '__main__':
    print get_token()



ps:


if __name__ == '__main__':
    print get_token()

    import requests


    def get_token():
        # 獲取token:呼叫get_token方法
        token_url = "http://api-zhike.vhall.com/initiator/live/get-token"
        token_data = {"activityId": "519903328"}
        token_headers = {"Cookie": "vmpbId=8442b04cd6728969de81935d5323cda6"}

        t = requests.post(token_url, data=token_data, headers=token_headers)
        token = t.json()
        return token["data"]


    if __name__ == '__main__':
        print get_token()


獲取cookie:cookie為請求頭中cookie

 

# -*- coding: utf-8 -*-
# @File    : get_Cookie.py
# @API_name:
# @Time    : 2018/12/11 10:35
# @Author  : guozhen.deng

#  獲取cookie,處理邏輯:
# 通過登入介面http://api*********.com/user/business-user/account-login
# 引數:account:******
# password:123saiya

import requests

def get_cookie(Coookie_url,Cookie_data):
    # Coookie_url = "http://*********v*********l.com/user/business-user/account-login"
    # Cookie_data = {
    #     "account" : '18*************94',
    #     "password" : '123*********a'
    # }
    r = requests.post(Coookie_url,data=Cookie_data)
    cookie = r.cookies.get_dict()

    return   "****=%s" %cookie["****"]

if __name__ == '__main__':
    print get_cookie()