From 2e8167fa3fa398505d4a3bb84de02388683d1f69 Mon Sep 17 00:00:00 2001 From: Mark Hilbush Date: Thu, 30 Jun 2022 13:03:06 -0400 Subject: [PATCH] [googlestt] Fix authorization from returning null refresh token (#13043) Signed-off-by: Mark Hilbush --- bundles/org.openhab.voice.googlestt/README.md | 2 +- .../voice/googlestt/internal/GoogleSTTService.java | 12 +++++++++--- .../src/main/resources/OH-INF/config/config.xml | 2 +- .../main/resources/OH-INF/i18n/googlestt.properties | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.voice.googlestt/README.md b/bundles/org.openhab.voice.googlestt/README.md index 21262fbdd..e56b2ad45 100644 --- a/bundles/org.openhab.voice.googlestt/README.md +++ b/bundles/org.openhab.voice.googlestt/README.md @@ -28,7 +28,7 @@ Using your favorite configuration UI to edit **Settings / Other Services - Googl * **Client Secret** - Google Cloud Platform OAuth 2.0-Client Secret. * **Authorization Code** - This code is used once for retrieving the Google Cloud Platform access and refresh tokens. **Please go to your browser ...** -[https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=](https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=) (replace `` by your Client Id) +[https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=](https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id=) (replace `` by your Client Id) **... to generate an authorization code and paste it here**. After your browser has been redirected to https://www.google.com, the authorization code will be set in the browser URL as value of the "code" URL query parameter. After initial authorization, this code is not needed anymore. diff --git a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java index 78f20a8f8..0cc8f93ff 100644 --- a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java +++ b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java @@ -175,7 +175,11 @@ public class GoogleSTTService implements STTService { private void getAccessToken(OAuthClientService oAuthService, String oauthCode) { logger.debug("Trying to get access and refresh tokens."); try { - oAuthService.getAccessTokenResponseByAuthorizationCode(oauthCode, GCP_REDIRECT_URI); + AccessTokenResponse response = oAuthService.getAccessTokenResponseByAuthorizationCode(oauthCode, + GCP_REDIRECT_URI); + if (response.getRefreshToken() == null || response.getRefreshToken().isEmpty()) { + logger.warn("Error fetching refresh token. Please try to reauthorize."); + } } catch (OAuthException | OAuthResponseException e) { if (logger.isDebugEnabled()) { logger.debug("Error fetching access token: {}", e.getMessage(), e); @@ -298,19 +302,21 @@ public class GoogleSTTService implements STTService { private @Nullable Credentials getCredentials() { String accessToken = null; + String refreshToken = null; try { OAuthClientService oAuthService = this.oAuthService; if (oAuthService != null) { AccessTokenResponse response = oAuthService.getAccessTokenResponse(); if (response != null) { accessToken = response.getAccessToken(); + refreshToken = response.getRefreshToken(); } } } catch (OAuthException | IOException | OAuthResponseException e) { logger.warn("Access token error: {}", e.getMessage()); } - if (accessToken == null) { - logger.warn("Missed google cloud access token"); + if (accessToken == null || refreshToken == null) { + logger.warn("Missed google cloud access and/or refresh token"); return null; } return OAuth2Credentials.create(new AccessToken(accessToken, null)); diff --git a/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/config/config.xml index 453035e81..5e3aa0cd8 100644 --- a/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/config/config.xml @@ -29,7 +29,7 @@ -
https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id={{clientId}}

After your browser has been redirected to https://www.google.com, the authorization code will be set in the browser URL as value of the "code" URL query parameter.]]>
+
https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id={{clientId}}

After your browser has been redirected to https://www.google.com, the authorization code will be set in the browser URL as value of the "code" URL query parameter.]]>
diff --git a/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/i18n/googlestt.properties b/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/i18n/googlestt.properties index cc6ddf689..83b3a80f2 100644 --- a/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/i18n/googlestt.properties +++ b/bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/i18n/googlestt.properties @@ -17,7 +17,7 @@ voice.config.googlestt.maxTranscriptionSeconds.description = Max seconds to wait voice.config.googlestt.noResultsMessage.label = No Results Message voice.config.googlestt.noResultsMessage.description = Message to be told when no results. (Empty for disabled) voice.config.googlestt.oauthCode.label = Authorization Code -voice.config.googlestt.oauthCode.description = This code is used once for retrieving the Google Cloud Platform access and refresh tokens. Open the following URL in your browser to generate an authorization code:

https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id={{clientId}}

After your browser has been redirected to https://www.google.com, the authorization code will be set in the browser URL as value of the "code" URL query parameter. +voice.config.googlestt.oauthCode.description = This code is used once for retrieving the Google Cloud Platform access and refresh tokens. Open the following URL in your browser to generate an authorization code:

https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/cloud-platform&access_type=offline&prompt=consent&include_granted_scopes=true&response_type=code&redirect_uri=https://www.google.com&client_id={{clientId}}

After your browser has been redirected to https://www.google.com, the authorization code will be set in the browser URL as value of the "code" URL query parameter. voice.config.googlestt.refreshSupportedLocales.label = Refresh Supported Locales voice.config.googlestt.refreshSupportedLocales.description = Try loading supported locales from the documentation page. voice.config.googlestt.singleUtteranceMode.label = Single Utterance Mode