googlefit-homeassistant icon indicating copy to clipboard operation
googlefit-homeassistant copied to clipboard

Error 400: invalid_request

Open baldachyn opened this issue 2 years ago • 11 comments

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?

baldachyn avatar Mar 19 '22 09:03 baldachyn

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.

nolander86 avatar Apr 21 '22 20:04 nolander86

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.

seanmhoffman avatar May 07 '22 05:05 seanmhoffman

Worked for me thanks!

Unthred avatar May 10 '22 12:05 Unthred

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'

baldachyn avatar May 10 '22 18:05 baldachyn

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

FirefighterRQ avatar Jun 10 '22 23:06 FirefighterRQ

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

ellite avatar Jun 17 '22 09:06 ellite

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

fcamilleri avatar Jul 06 '22 09:07 fcamilleri

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.

IvanVojtko avatar Jul 17 '22 10:07 IvanVojtko

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 avatar Sep 16 '22 18:09 grandles1212

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

david-romero avatar Nov 10 '22 09:11 david-romero

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.

kingozone avatar Dec 18 '22 23:12 kingozone