Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google_alerts.InvalidState: State was not properly obtained from the app #40

Open
serencha opened this issue May 10, 2021 · 1 comment

Comments

@serencha
Copy link

serencha commented May 10, 2021

OS: Windows 10
Browser: Chrome
Version: 0.2.9

When I run this:

from google_alerts import GoogleAlerts

ga = GoogleAlerts(email, password)
ga.authenticate()
ga.list()

I get this output:

←[1;32mDEBUG __init__:_config_bootstrap():156 2021-05-10 18:33:25,930←[0m| Caching authentication in config file
←[1;32mDEBUG __init__:_session_check():185 2021-05-10 18:33:25,930←[0m| Loaded cookies from session file
←[1;32mDEBUG __init__:_process_state():225 2021-05-10 18:33:26,460←[0m| Capturing state from the request
←[1;32mDEBUG __init__:authenticate():291 2021-05-10 18:33:27,202←[0m| [!] User has already authenticated

... and then I get an error about InvalidState. I have already saved my config and seeded my session. I have not added an alert - this is my first time using this API.

How should I get around this error?

@shane-staret
Copy link

shane-staret commented Mar 22, 2023

I apologize, this is incredibly late, but in case it helps someone in the future...

This error is occurring because the process_state() function in the __init__.py file is outdated. It's attempting to pull the state from the text of the HTML tag, but Google Alerts interface has changed so that the text of the tag is blank. Instead, you need to pull the state from the property of the tag itself. This requires code changes in the process_state() function.

On line 228, you will see for i in soup.findAll('script'):

Edit this for loop to contain the following:

        for i in soup.findAll('script'):
            string_i = str(i)
            if 'window.STATE' not in string_i:
                continue
            try:
                state = json.loads(string_i[string_i.index('window.STATE') + 13:-15])
                if state != "":
                    self._state = state
                    self._log.debug("State value set: %s" % self._state)
            except Exception as e:
                raise StateParseFailure("Google has changed their core protocol and a new parser must be built. Please file a bug at https://github.com/9b/google-alerts/issues.")

For context, this is what the outdated for loop looks like currently:

        for i in soup.findAll('script'):
            if i.text.find('window.STATE') == -1:
                continue
            try:
                state = json.loads(i.text[25:-6])
                if state != "":
                    self._state = state
                    self._log.debug("State value set: %s" % self._state)
            except Exception as e:
                raise StateParseFailure("Google has changed their core protocol and a new parser must be built. Please file a bug at https://github.com/9b/google-alerts/issues.")

Please let me know if this was helpful! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants