[googlestt] Fix authorization from returning null refresh token (#13043)

Signed-off-by: Mark Hilbush <mark@hilbush.com>
This commit is contained in:
Mark Hilbush 2022-06-30 13:03:06 -04:00 committed by GitHub
parent 00400562d4
commit 2e8167fa3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 6 deletions

View 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. * **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. * **Authorization Code** - This code is used once for retrieving the Google Cloud Platform access and refresh tokens.
**Please go to your browser ...** **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**. **... 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 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. After initial authorization, this code is not needed anymore.

View File

@ -175,7 +175,11 @@ public class GoogleSTTService implements STTService {
private void getAccessToken(OAuthClientService oAuthService, String oauthCode) { private void getAccessToken(OAuthClientService oAuthService, String oauthCode) {
logger.debug("Trying to get access and refresh tokens."); logger.debug("Trying to get access and refresh tokens.");
try { 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) { } catch (OAuthException | OAuthResponseException e) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Error fetching access token: {}", e.getMessage(), e); logger.debug("Error fetching access token: {}", e.getMessage(), e);
@ -298,19 +302,21 @@ public class GoogleSTTService implements STTService {
private @Nullable Credentials getCredentials() { private @Nullable Credentials getCredentials() {
String accessToken = null; String accessToken = null;
String refreshToken = null;
try { try {
OAuthClientService oAuthService = this.oAuthService; OAuthClientService oAuthService = this.oAuthService;
if (oAuthService != null) { if (oAuthService != null) {
AccessTokenResponse response = oAuthService.getAccessTokenResponse(); AccessTokenResponse response = oAuthService.getAccessTokenResponse();
if (response != null) { if (response != null) {
accessToken = response.getAccessToken(); accessToken = response.getAccessToken();
refreshToken = response.getRefreshToken();
} }
} }
} catch (OAuthException | IOException | OAuthResponseException e) { } catch (OAuthException | IOException | OAuthResponseException e) {
logger.warn("Access token error: {}", e.getMessage()); logger.warn("Access token error: {}", e.getMessage());
} }
if (accessToken == null) { if (accessToken == null || refreshToken == null) {
logger.warn("Missed google cloud access token"); logger.warn("Missed google cloud access and/or refresh token");
return null; return null;
} }
return OAuth2Credentials.create(new AccessToken(accessToken, null)); return OAuth2Credentials.create(new AccessToken(accessToken, null));

View File

@ -29,7 +29,7 @@
</parameter> </parameter>
<parameter name="oauthCode" type="text" groupName="authentication"> <parameter name="oauthCode" type="text" groupName="authentication">
<label>Authorization Code</label> <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>
<parameter name="singleUtteranceMode" type="boolean" groupName="stt"> <parameter name="singleUtteranceMode" type="boolean" groupName="stt">
<label>Single Utterance Mode</label> <label>Single Utterance Mode</label>

View 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.label = No Results Message
voice.config.googlestt.noResultsMessage.description = Message to be told when no results. (Empty for disabled) 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.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.label = Refresh Supported Locales
voice.config.googlestt.refreshSupportedLocales.description = Try loading supported locales from the documentation page. voice.config.googlestt.refreshSupportedLocales.description = Try loading supported locales from the documentation page.
voice.config.googlestt.singleUtteranceMode.label = Single Utterance Mode voice.config.googlestt.singleUtteranceMode.label = Single Utterance Mode