尝试从URL API中提取JSON文件...我认为即时通讯无法保持会话正确打开

时间:2019-09-17 14:05:46

标签: python json api python-requests

我能够使登录api工作,获取cookie,但是遇到的错误是当我尝试调用json时,它从None返回JSONDecodeError(“ Expecting value”,s,err.value),因为当我确实执行r.text,它返回登录页面的html。...因此它不维护会话,因此无法调用试图调用的特定API url。

from datetime import datetime
import json
import time
import sys
import requests

print("1")
base_url = "http://ludwig.#######.com:####/###"
print("2")

def _url(path):
    return base_url + path

# 1st try: get the cookies from the session and pass them to following  requests
def get_cookie(username, password):
    url = _url('/j_spring_security_check?j_username=username&j_password=password')
    session = requests.Session()
    session.cookies.get_dict()
    response = session.get(url)

    return session.cookies.get_dict()

print("3")
username = "####"
password = "####"
cookies = get_cookie(username, password)
print("4")
headers = {'accept': 'application/json'}
print("5")

def loginAPI():
    url = "http://ludwig.######.com:####/###"
    urlAPI = url + '/j_spring_security_check?j_username=####3&j_password=#####'
    s = requests.session()
    # Post a login request to Podium App
    data = {'Username': '#####',
            'Password': '#####'}
    loginResponse = s.post(urlAPI, data)
    loginResponseText = loginResponse.text
    if not "login" in loginResponseText:
        print("Login successful")

        return s
    # If not login, then report error and exit
    else:
        print("Invalid username or password....!!")

        exit(1)


print("6")

url_cookie = 'http://####.com:####/qdc/j_spring_security_check?j_username_plain=#####&j_password=####'
url_get = 'http://######.com:####/qdc/entity/v1/getEntities?type=EXTERNAL&count=2&sortAttr=name&sortDir=ASC'
session = requests.Session()

r = requests.get(url_get, auth=(username,password), headers={'Accept': 'application/json'},verify=False)

print('--------------------- 1. status_code ----------------------------------')
print(r.status_code)
print('--------------------- 1. headers ----------------------------------')
print(r.headers)
print('--------------------- 1. content ----------------------------------')
data1=r.json()
print(data1)

0 个答案:

没有答案