def login(self, username, password, gmail, gpasswd): """Login to Snapchat account Returns a dict containing user information on successful login, the data returned is similar to get_updates. :param username Snapchat username :param password Snapchat password :param gmail Gmail address :param gpasswd Gmail password """ self.gmail = gmail self.gpasswd = gpasswd i = 0 logged_in = False while i < 4 and logged_in == False: i += 1 now = str(timestamp()) req_token = make_request_token(STATIC_TOKEN, now) gauth_token = get_auth_token(gmail, gpasswd) self.gauth = gauth_token[0] self.expiry = gauth_token[1] string = username + "|" + password + "|" + now + "|" + req_token dtoken = self._get_device_token() self._unset_auth() attestation = get_attestation(username, password, now) r = self._request('/loq/login', { 'username': username, 'password': password, 'height': 1280, 'width': 720, 'max_video_height': 640, 'max_video_width': 480, 'dsig': hmac.new(str(dtoken['dtoken1v']),string,sha256).hexdigest()[:20], 'dtoken1i': dtoken['dtoken1i'], 'ptoken': "ie", 'attestation': attestation, 'sflag': 1, 'application_id': 'com.snapchat.android', 'req_token': req_token }, { 'now': now, 'gauth': self._get_gauth() }, None, True, 'post', { 'X-Snapchat-Client-Auth': get_client_auth_token(username, password, now)['signature'] }) result = r.json() if 'updates_response' in result: logged_in = True if 'auth_token' in result['updates_response']: self.auth_token = result['updates_response']['auth_token'] if 'username' in result['updates_response']: self.username = username if self.username is None and self.auth_token is None: raise Exception(result.get('message', 'unknown error')) return result
def _get_gauth(self): """This is the proper way to access self.gauth when using it for an API request. This first checks to see if the Bearer token is expired, renewing it if needed. """ if datetime.now() >= self.expiry: gauth_token = get_auth_token(self.gmail, self.gpasswd) self.gauth = gauth_token[0] self.expiry = gauth_token[1] return self.gauth
def restore_token(self, username, auth_token, gmail, gpasswd): """Restore a Snapchat session from an auth_token parameter returned in the response of a login request. Useful for when Snapchat breaks the login API. :param username Snapchat username :param auth_token Snapchat auth_token :param gmail Gmail address :param gpasswd Gmail password """ self.username = username self.auth_token = auth_token self.gmail = gmail self.gpasswd = gpasswd gauth_token = get_auth_token(gmail, gpasswd) self.gauth = gauth_token[0] self.expiry = gauth_token[1]
def login(self, username, password, gmail, gpasswd): """Login to Snapchat account Returns a dict containing user information on successful login, the data returned is similar to get_updates. :param username Snapchat username :param password Snapchat password :param gmail Gmail address :param gpasswd Gmail password """ self.gmail = gmail self.gpasswd = gpasswd i = 0 logged_in = False while i < 4 and logged_in == False: i += 1 now = str(timestamp()) req_token = make_request_token(STATIC_TOKEN, now) gauth_token = get_auth_token(gmail, gpasswd) self.gauth = gauth_token[0] self.expiry = gauth_token[1] string = username + "|" + password + "|" + now + "|" + req_token dtoken = self._get_device_token() self._unset_auth() attestation = get_attestation(username, password, now) r = self._request( '/loq/login', { 'username': username, 'password': password, 'height': 1280, 'width': 720, 'max_video_height': 640, 'max_video_width': 480, 'dsig': hmac.new(str(dtoken['dtoken1v']), string, sha256).hexdigest()[:20], 'dtoken1i': dtoken['dtoken1i'], 'ptoken': "ie", 'attestation': attestation, 'sflag': 1, 'application_id': 'com.snapchat.android', 'req_token': req_token }, { 'now': now, 'gauth': self._get_gauth() }, None, True, 'post', { 'X-Snapchat-Client-Auth': get_client_auth_token(username, password, now)['signature'] }) result = r.json() if 'updates_response' in result: logged_in = True if 'auth_token' in result['updates_response']: self.auth_token = result['updates_response']['auth_token'] if 'username' in result['updates_response']: self.username = username if self.username is None and self.auth_token is None: raise Exception(result.get('message', 'unknown error')) return result