googlefit-homeassistant
googlefit-homeassistant copied to clipboard
Error 400: invalid_request
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?
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.
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:
- Follow the steps listed under the Generate Client ID and Client Secret section here until you reach Step 13.
- Set the Application type to
Web Application
and give this credential set a name (like Home Assistant Fit Credentials) - Under the Authorized redirect URIs section, click
+ Add URI
- 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
orhttps://<HOME_ASSISTANT_EXTERNAL_URL>/auth/external/callback
- Click
Create
- Download the new credentials and save them as
credentials.json
Generate Token:
- In the same directory that
credentials.json
is located download the updatedget_credentials.py
from this gist. - Open
get_credentials.py
in a text editor and update Line 21 to your redirect url and save. - Next in your terminal navigate to the directory where your files are located and run
python get_credentials.py
- Navigate to the url generated in the terminal output.
- Follow the steps to authenticate.
If you run into the Google hasn’t verified this app page, click the
Advanced
link and theGo to (xxxx)
- When you reach a white page that says
Invalid state
copy the Authenticate Code from the URL. It is located between thestate
andscope
url parameters and should be prefixed withcode=
and end before&scope
- Enter the code taken from the url into the
Enter the authorization code:
prompt in your terminal and clickEnter
- Copy the newly generated
.google_fit.token
into your HA configuration directory and restart HA.
Worked for me thanks!
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'
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
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
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
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.
I'll update a README file.
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 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
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.