IvanVojtko / googlefit-homeassistant

MIT License
27 stars 10 forks source link

Error 400: invalid_request #10

Open baldachyn opened 2 years ago

baldachyn commented 2 years ago

After couple of days working the integration stopped providing data, log says:

google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

while trying to re-authenticate with google, google says

You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure.

I am only one experiencing this?

nolander86 commented 2 years ago

It uses the OOB flow which has been deprecated for any new clients it looks like. https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html#disallowed-oob.

seanmhoffman commented 2 years ago

The workaround that I found to this currently is available to anyone who uses Nabu Casa or has their HA install available outside of their home network via DuckDNS or some other method.

Generate Client ID & Secret:

  1. Follow the steps listed under the Generate Client ID and Client Secret section here until you reach Step 13.
  2. Set the Application type to Web Application and give this credential set a name (like Home Assistant Fit Credentials)
  3. Under the Authorized redirect URIs section, click + Add URI
  4. In the input field that appears enter the OAuth callback URL for your HA installation i.e. https://<NABU_CASA_ID>.ui.nabu.casa/auth/external/callback or https://<HOME_ASSISTANT_EXTERNAL_URL>/auth/external/callback
  5. Click Create
  6. Download the new credentials and save them as credentials.json

Generate Token:

  1. In the same directory that credentials.json is located download the updated get_credentials.py from this gist.
  2. Open get_credentials.py in a text editor and update Line 21 to your redirect url and save.
  3. Next in your terminal navigate to the directory where your files are located and run python get_credentials.py
  4. Navigate to the url generated in the terminal output.
  5. Follow the steps to authenticate. If you run into the Google hasn’t verified this app page, click the Advanced link and the Go to (xxxx)
  6. When you reach a white page that says Invalid state copy the Authenticate Code from the URL. It is located between the state and scope url parameters and should be prefixed with code= and end before &scope
  7. Enter the code taken from the url into the Enter the authorization code: prompt in your terminal and click Enter
  8. Copy the newly generated .google_fit.token into your HA configuration directory and restart HA.
Unthred commented 2 years ago

Worked for me thanks!

baldachyn commented 2 years ago

Didn't work for me, though :-( When ran get_credentials.py it printed Traceback (most recent call last): File "C:\Users\tomas\Downloads\google fit2\get_credentials.py", line 3, in <module> from google_auth_oauthlib.flow import Flow ModuleNotFoundError: No module named 'google_auth_oauthlib'

FirefighterRQ commented 2 years ago

Didn't work for me, though :-( When ran get_credentials.py it printed Traceback (most recent call last): File "C:\Users\tomas\Downloads\google fit2\get_credentials.py", line 3, in <module> from google_auth_oauthlib.flow import Flow ModuleNotFoundError: No module named 'google_auth_oauthlib'

I get the same error when I run it

ellite commented 2 years ago

Didn't work for me, though :-( When ran get_credentials.py it printed Traceback (most recent call last): File "C:\Users\tomas\Downloads\google fit2\get_credentials.py", line 3, in <module> from google_auth_oauthlib.flow import Flow ModuleNotFoundError: No module named 'google_auth_oauthlib'

I get the same error when I run it

Just run: pip3 install google_auth_oauthlib

fcamilleri commented 2 years ago

Thanks ! It's working now, however I had to set the Application Type to 'Desktop Application' instead of 'Web Application' otherwise Google would block the auth

IvanVojtko commented 2 years ago

Hey @baldachyn. I updated get_credentials script but I also had to generate new credentials in google fitness API. I had to choose Desktop application, otherwise it was not working. image I'll update a README file.

grandles1212 commented 2 years ago

I was able to get the token file, but when I upload it into home assistant, I get the error below. Any ideas?


Traceback (most recent call last): File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform await asyncio.shield(task) File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run result = self.fn(*self.args, **self.kwargs) File "/home/homeassistant/.homeassistant/custom_components/google_fit/sensor.py", line 176, in setup_platform client = _get_client(token_file) File "/home/homeassistant/.homeassistant/custom_components/google_fit/sensor.py", line 98, in _get_client http = credentials.authorize(httplib2.Http()) AttributeError: 'NoneType' object has no attribute 'authorize'

david-romero commented 1 year ago

I was able to get the token file, but when I upload it into home assistant, I get the error below. Any ideas?

Traceback (most recent call last): File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform await asyncio.shield(task) File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run result = self.fn(*self.args, **self.kwargs) File "/home/homeassistant/.homeassistant/custom_components/google_fit/sensor.py", line 176, in setup_platform client = _get_client(token_file) File "/home/homeassistant/.homeassistant/custom_components/google_fit/sensor.py", line 98, in _get_client http = credentials.authorize(httplib2.Http()) AttributeError: 'NoneType' object has no attribute 'authorize'

@grandles1212

I have the same issue

kingozone commented 1 year ago

I was able to get the token file, but when I upload it into home assistant, I get the error below. Any ideas?

Traceback (most recent call last): File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform await asyncio.shield(task) File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run result = self.fn(*self.args, **self.kwargs) File "/home/homeassistant/.homeassistant/custom_components/google_fit/sensor.py", line 176, in setup_platform client = _get_client(token_file) File "/home/homeassistant/.homeassistant/custom_components/google_fit/sensor.py", line 98, in _get_client http = credentials.authorize(httplib2.Http()) AttributeError: 'NoneType' object has no attribute 'authorize'

I too, have the same issue.