使用Python验证PowerSchool

使用Python验证PowerSchool,python,post,python-requests,powerschool,Python,Post,Python Requests,Powerschool,这件事困扰了我一段时间。我想写一个程序,这样我就可以自动登录到我的PowerSchool门户网站,将来它可能会让我做一些事情,比如解析我的时间表和成绩。第一步是认证,这对我来说是个问题 import sys import os import requests import lxml import json from bs4 import BeautifulSoup def login(username, password): with requests.Session() as s:

这件事困扰了我一段时间。我想写一个程序,这样我就可以自动登录到我的PowerSchool门户网站,将来它可能会让我做一些事情,比如解析我的时间表和成绩。第一步是认证,这对我来说是个问题

import sys
import os
import requests
import lxml
import json
from bs4 import BeautifulSoup


def login(username, password):
    with requests.Session() as s:
        url = 'https://sisavrsb.ednet.ns.ca/guardian/home.html#sign-in-content'
        r = s.get(url)
        soup = BeautifulSoup(r.text, "lxml")
        token = soup.select_one("[name='pstoken']")['value']
        contextdata = soup.select_one("[name='contextData']")['value']
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'en-US,en;q=0.5',
            'Connection': 'keep-alive',
            #'Content-Length': '423',
            #'Content-Type': 'application/x-www-form-urlencoded',
            #'Cookie': 'JSESSIONID=0B1666C446234245CECC2983F1D6CA8A; PowerSchool_Cookie_K=2069644430.1.329063952.2221457792',
            'DNT': '1',
            #'Host': 'sisavrsb.ednet.ns.ca',
            'Referer': 'https://sisavrsb.ednet.ns.ca/public/',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0'
        }
        print(contextdata)
        data = json.dumps({
            'account': username,
            #'contextData': '30A7205567365DDB643E707E25B32D43578D70A04D9F407113CF640632082056',
            'contextData' : contextdata,
            'credentialType': 'User Id and Password Credential',
            #'dbpw': '61a2474517a2f79ae0da0781b9bdf57d',
            #'dbpw' : password,
            'pcasServerUrl': '\/',
            'pstoken': token,
            'pw': password,
            'returnUrl': '',
            'serviceName': 'PS Parent Portal',
            'serviceTicket': '',
            'translator_ldappassword': '',
            'translator_password': '',
            'translator_username': '',
            'translatorpw': ''

        })
        p = s.post(url, headers=headers, data=data, allow_redirects=True)
        soup = BeautifulSoup(p.text, "lxml")
        if p.status_code == 302:
            print('Success!')
        else:
            print('Authentication error', p.status_code)
            print('cookies', requests.utils.dict_from_cookiejar(s.cookies))
            print(p.history)
            print(p.headers)

def main():
    login('xxxxx', 'xxxxx')


if __name__ == '__main__':
    main()
在这一点上,我几乎尝试了从Mechanize到(过时的)PowerSchool API的所有方法。我已经尽了最大努力,使用
requests.Session()
复制了头和数据,以便cookie正常工作。经过几个小时的摆弄,我终于得到了它,因此p.history()不是空白的。它现在包含“
”,这对我来说非常模糊,但总比什么都没有好

这是我的输出

Authentication error 200
cookies {'JSESSIONID': 'B847F853CC373DC7EAA8800FA02EEC00', 'PowerSchool_Cookie_K': '2069644430.1.329063608.2225303936'}
[<Response [302]>]
{'Server': 'Apache-Coyote/1.1', 'Cache-control': 'no-store, no-cache, must-revalidate, post-check=0, check=0', 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT', 'Content-Type': 'text/html;charset=UTF-8', 'Content-Length': '8238', 'Date': 'Thu, 08 Feb 2018 01:01:05 GMT'}

编辑:机器人浏览器也给了我500的回复。想知道这是因为我遗漏了什么,还是仅仅是他们那边的一个问题。

不确定这是否适用于你,但我最近做了类似的事情

所有的逻辑都在文件中完成(这似乎是一个他们修改并添加了自己函数的库)

这是我在自己的脚本中使用的python

from bs4 import BeautifulSoup
import requests, base64, hashlib, hmac

POWERSCHOOL_BASE_URL = "https://powerschool.eips.ca/"

def initLoginPage(httpSession: requests.Session) -> [str, str]:
    response = httpSession.get(POWERSCHOOL_BASE_URL + "public/home.html")
    html_response = BeautifulSoup(response.content, "lxml")

    contextData = html_response.find('input', id='contextData').attrs['value']
    pstoken = html_response.find('input', attrs={'name': 'pstoken'}).attrs['value']

    return contextData, pstoken

def getPassword(contextData: str, password: str) -> str:
    return hmac.new(contextData.encode('UTF-8'), msg=base64.b64encode(hashlib.md5(password.encode('UTF-8')).digest()).strip(b'='), digestmod=hashlib.md5).hexdigest()

def login(httpSession: requests.Session, username: str, pw: str, pstoken: str) -> requests.Response:
    post_data = {
        'account': username,
        'pw': pw,
        'pstoken': pstoken,
    }
    return httpSession.post(POWERSCHOOL_BASE_URL + "guardian/home.html", data=post_data)

旁注:整个PowerSchool系统非常、非常混乱且不安全(以防仅仅对一行代码进行密码哈希运算是不够的)。

这需要大量的调试。您是否考虑过使用更像浏览器的工具?例如,我正在考虑使用无头浏览器的Selenium。@HubertGrzeskowiak我已经尝试了RoboBrowser,但它不起作用。我还想在我的远程服务器上运行它,所以如果它不像Selenium那样依赖安装的Firefox gecko驱动程序,那就更好了。
from bs4 import BeautifulSoup
import requests, base64, hashlib, hmac

POWERSCHOOL_BASE_URL = "https://powerschool.eips.ca/"

def initLoginPage(httpSession: requests.Session) -> [str, str]:
    response = httpSession.get(POWERSCHOOL_BASE_URL + "public/home.html")
    html_response = BeautifulSoup(response.content, "lxml")

    contextData = html_response.find('input', id='contextData').attrs['value']
    pstoken = html_response.find('input', attrs={'name': 'pstoken'}).attrs['value']

    return contextData, pstoken

def getPassword(contextData: str, password: str) -> str:
    return hmac.new(contextData.encode('UTF-8'), msg=base64.b64encode(hashlib.md5(password.encode('UTF-8')).digest()).strip(b'='), digestmod=hashlib.md5).hexdigest()

def login(httpSession: requests.Session, username: str, pw: str, pstoken: str) -> requests.Response:
    post_data = {
        'account': username,
        'pw': pw,
        'pstoken': pstoken,
    }
    return httpSession.post(POWERSCHOOL_BASE_URL + "guardian/home.html", data=post_data)