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 #36

Open
LuisBenitez-stradata opened this issue Mar 9, 2021 · 2 comments

Comments

@LuisBenitez-stradata
Copy link

Hello.

When I try to request the alert list (or perform any action) I get the following message: google_alerts.InvalidState: State was not properly obtained from the app

I have already logged in and have only created an alert through the web interface. The login is successful.

Below details of the error:

DEBUG __init__:_config_bootstrap():156 2021-03-09 14:12:13,183| Caching authentication in config file
DEBUG __init__:_session_check():185 2021-03-09 14:12:13,187| Loaded cookies from session file
DEBUG __init__:_process_state():225 2021-03-09 14:12:14,003| Capturing state from the request
Traceback (most recent call last):
  File "E:/LuisBenitez/Proyectos/google_alerts/test.py", line 6, in <module>
    ga.list()
  File "C:\ProgramData\Anaconda3\lib\site-packages\google_alerts\__init__.py", line 333, in list
    raise InvalidState("State was not properly obtained from the app")
google_alerts.InvalidState: State was not properly obtained from the app
DEBUG __init__:authenticate():291 2021-03-09 14:12:15,039| [!] User has already authenticated
@fmtsvetkov
Copy link
Contributor

I have the same error and don't know how to fix it. Using this package is critical for my study. If @9b have any suggestions, that will great

@shanestaret
Copy link

shanestaret 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

3 participants