-
Notifications
You must be signed in to change notification settings - Fork 121
/
api.py
100 lines (81 loc) · 3.13 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import requests
import re
import struct
import json
import argparse
import random
import logging
from session import PogoSession
import location
from gpsoauth import perform_master_login, perform_oauth
API_URL = 'https://pgorelease.nianticlabs.com/plfe/rpc'
LOGIN_URL = 'https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize'
LOGIN_OAUTH = 'https://sso.pokemon.com/sso/oauth2.0/accessToken'
PTC_CLIENT_SECRET = 'w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR'
ANDROID_ID = '9774d56d682e549c'
SERVICE= 'audience:server:client_id:848232511240-7so421jotr2609rmqakceuu1luuq0ptb.apps.googleusercontent.com'
APP = 'com.nianticlabs.pokemongo'
CLIENT_SIG = '321187995bc7cdc2b5fc91b11a96e2baa8602c62'
RPC_ID = int(random.random() * 10 ** 12)
def getRPCId():
global RPC_ID
RPC_ID = RPC_ID + 1
return RPC_ID
def createRequestsSession():
session = requests.session()
session.headers = {
'User-Agent': 'Niantic App',
}
session.verify = False
return session
def createPogoSession(session, provider, access_token, loc):
loc = location.getLocation(loc)
if loc:
logging.info('Location: {}'.format(loc.address))
logging.info('Coordinates: {} {} {}'.format(loc.latitude, loc.longitude,
loc.altitude))
if access_token and loc:
return PogoSession(session, provider, access_token, loc)
elif loc is None:
logging.critical('Location not found')
elif access_token is None:
logging.critical('Access token not generated')
return None
def createGoogleSession(username, pw, startLocation):
session = createRequestsSession()
logging.info('Creating Google session for {}'.format(username))
r1 = perform_master_login(username, pw, ANDROID_ID)
r2 = perform_oauth(username, r1.get('Token', ''), ANDROID_ID, SERVICE, APP,
CLIENT_SIG)
access_token = r2.get('Auth') # access token
return createPogoSession(session, 'google', access_token, startLocation)
def createPTCSession(username, pw, startLocation):
session = createRequestsSession()
logging.info('Creating PTC session for {}'.format(username))
r = session.get(LOGIN_URL)
jdata = json.loads(r.content)
data = {
'lt': jdata['lt'],
'execution': jdata['execution'],
'_eventId': 'submit',
'username': username,
'password': pw,
}
authResponse = session.post(LOGIN_URL, data=data)
ticket = None
try:
ticket = re.sub('.*ticket=', '', authResponse.history[0].headers['Location'])
except Exception as e:
logging.error(authResponse.json()['errors'][0])
return None
data1 = {
'client_id': 'mobile-app_pokemon-go',
'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error',
'client_secret': PTC_CLIENT_SECRET,
'grant_type': 'refresh_token',
'code': ticket,
}
r2 = session.post(LOGIN_OAUTH, data=data1)
access_token = re.sub('&expires.*', '', r2.content)
access_token = re.sub('.*access_token=', '', access_token)
return createPogoSession(session, 'ptc', access_token, startLocation)