获取 token
1.1 获取 npsso
使用浏览器访问 playstation 官网登录账号(国服、港服都可以),之后在同一个浏览器(注意不要使用浏览器的无痕模式)中再访问 https://ca.account.sony.com/api/v1/ssocookie 获取 npsso。
1.2 获取 auth_code
import requests
from urllib.parse import parse_qs, urlparse
# 1.1 中获取的 npsso
npsso = "xrvW*****************************NYjq"
auth_url = "https://ca.account.sony.com/api/authz/v3/oauth/authorize"
# 固定参数
params = {
"access_type": "offline",
"client_id": "09515159-7237-4370-9b40-3806e67c0891",
"response_type": "code",
"scope": "psn:mobile.v2.core psn:clientapp",
"redirect_uri": "com.scee.psxandroid.scecompcall://redirect",
}
auth_url += "?" + "&".join([f"{key}={value}" for key, value in params.items()])
response = requests.get(auth_url, cookies={"npsso": npsso}, allow_redirects=False)
# 请求的 status code 为 302,auth_code 在 response headers 中
if response.status_code == 302:
location = response.headers.get("Location")
query = parse_qs(urlparse(location).query)
auth_code = query.get("code")[0]
1.3 获取 access_token 及 refresh_token
token_url = "https://ca.account.sony.com/api/authz/v3/oauth/token"
# 1.2 中获取的 auth_code
token_data = {
"code": auth_code,
"redirect_uri": "com.scee.psxandroid.scecompcall://redirect",
"grant_type": "authorization_code",
"token_format": "jwt",
}
# 请求头为固定内容
headers = {
"Authorization": "Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A=",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "PlayStation/21090100 CFNetwork/1126 Darwin/19.5.0",
}
response = requests.post(token_url, data=token_data, headers=headers)
response.raise_for_status()
# 请求成功后返回 access_token 及 refresh_token
access_token = response.json().get("access_token")
refresh_token = response.json().get("refresh_token")
access_token 时效只有 1 小时,refresh_token 的时效有 60 天,所以当 access_token 失效后,需要通过 refresh_token 来刷新 access_token。
1.4 刷新 access_token
token_url = "https://ca.account.sony.com/api/authz/v3/oauth/token"
# 1.1 中获取的 npsso
headers = {
"Authorization": "Basic MDk1MTUxNTktNzIzNy00MzcwLTliNDAtMzgwNmU2N2MwODkxOnVjUGprYTV0bnRCMktxc1A=",
"Cookie": f"npsso={npsso}",
"User-Agent": "PlayStation/21090100 CFNetwork/1126 Darwin/19.5.0",
}
# 1.3 获取的 refresh_token
token_data = {
"refresh_token": refresh_token,
"scope": "psn:mobile.v2.core psn:clientapp",
"grant_type": "refresh_token",
"token_format": "jwt",
}
response = requests.post(token_url, data=token_data, headers=headers)
response.raise_for_status()
# 请求成功后获取 access_token
access_token = response.json().get("access_token")