· 6 years ago · Mar 15, 2019, 07:04 AM
1async def _login(self, login_token, code=None):
2 logger.info(f'[PLAYSTATION NETWORK API] Logging in into PSN Account...')
3 r"""
4 Login using PSN credentials to access Playstation Network API.
5
6 :return:
7 """
8 async with aiohttp.ClientSession() as session:
9 if not code:
10 auth_data = {
11 'app_context': 'inapp_ios',
12 'client_id': self._CLIENT_ID,
13 'client_secret': self._CLIENT_SECRET,
14 'refresh_token': login_token,
15 'duid': self._DUID,
16 'grant_type': 'refresh_token',
17 'scope': self._SCOPE
18 }
19 async with session.post(self._AUTH_API + 'oauth/token', data=auth_data) as response:
20 data = json.loads(await response.text())
21 else:
22 auth_data = {
23 'authentication_type': 'two_step',
24 'ticket_uuid': login_token,
25 'code': code,
26 'client_id': self._CLIENT_ID
27 }
28 async with session.post(self._AUTH_API + 'ssocookie', data=auth_data) as response:
29 npsso = json.loads(await response.text())['npsso']
30
31 auth_data = {
32 'duid': self._DUID,
33 'client_id': self._CLIENT_ID,
34 'response_type': 'code',
35 'scope': self._SCOPE,
36 'redirect_uri': 'com.playstation.PlayStationApp://redirect'
37 }
38 headers = {
39 'Cookie': 'npsso=' + npsso
40 }
41 async with session.get(self._AUTH_API + 'oauth/authorize', params=auth_data, headers=headers,
42 allow_redirects=False) as response:
43 grant = response.headers['X-NP-GRANT-CODE']
44 auth_data = {
45 'client_id': self._CLIENT_ID,
46 'client_secret': self._CLIENT_SECRET,
47 'duid': self._DUID,
48 'scope': self._SCOPE,
49 'redirect_uri': 'com.playstation.PlayStationApp://redirect',
50 'code': grant,
51 'grant_type': 'authorization_code'
52 }
53 async with session.post(self._AUTH_API + 'oauth/token', data=auth_data) as response:
54 data = json.loads(await response.text())
55
56 print('Access Token: ' + data['access_token'])
57 print('Refresh Token: ' + data['refresh_token'])
58 print('Expires At: ' + str(data['expires_in']))
59
60 self._access_token = data['access_token']
61 self._refresh_token = data['refresh_token']
62 self._expires_at = time.time() + int(data['expires_in']) - 60
63
64 logger.info('[PLAYSTATION NETWORK API] Successfully logged in into Playstation Network Account.')