]> git.basschouten.com Git - openhab-addons.git/commitdiff
[googlestt] Fix authorization from returning null refresh token (#13043)
authorMark Hilbush <mark@hilbush.com>
Thu, 30 Jun 2022 17:03:06 +0000 (13:03 -0400)
committerGitHub <noreply@github.com>
Thu, 30 Jun 2022 17:03:06 +0000 (19:03 +0200)
Signed-off-by: Mark Hilbush <mark@hilbush.com>
bundles/org.openhab.voice.googlestt/README.md
bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java
bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/config/config.xml
bundles/org.openhab.voice.googlestt/src/main/resources/OH-INF/i18n/googlestt.properties

index 21262fbdd441405eaa7caa42566577f6155e445c..e56b2ad454bc2495ad264e9fcbee26b631955c90 100644 (file)
@@ -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=<clientId>](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>) (replace `<clientId>` 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=<clientId>](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>) (replace `<clientId>` 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.
index 78f20a8f86c9e20655475e12c3bb4e92fb2752c4..0cc8f93ff11b2c5f577faa60e214915725eacbd9 100644 (file)
@@ -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));
index 453035e81b9534d36c9be2e602a2bccaad03224c..5e3aa0cd8435a8dfc2ccedaf9caebf3f2a8a8be3 100644 (file)
@@ -29,7 +29,7 @@
                </parameter>
                <parameter name="oauthCode" type="text" groupName="authentication">
                        <label>Authorization Code</label>
-                       <description><![CDATA[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:<br><br>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}}<br><br>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.]]></description>
+                       <description><![CDATA[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:<br><br>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}}<br><br>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.]]></description>
                </parameter>
                <parameter name="singleUtteranceMode" type="boolean" groupName="stt">
                        <label>Single Utterance Mode</label>
index cc6ddf689dc300efbbdd0f90e57f6d1fde054e85..83b3a80f2404b29b3edc6a2b654e3e39fadbbca6 100644 (file)
@@ -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:<br><br>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}}<br><br>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:<br><br>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}}<br><br>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