【发布时间】:2018-12-14 05:58:08
【问题描述】:
我正在使用 Android 上的 Google Speech to Text 服务将语音流式传输到文本。 Here is official sample repository. 样本运行成功。但是或者,您应该在服务器端获取访问令牌,并提供给客户端应用程序。
所以我设置了一个服务器并为应用程序创建 RESTful api 以获取访问令牌。
{"token":"1234567890sdertyuikjhgfghjk....."}
并且还使用 api:https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={token} 来获取令牌信息:
{
"issued_to": "102073270616313859663",
"audience": "102073270616313859663",
"scope": "https://www.googleapis.com/auth/cloud-platform",
"expires_in": 3600,
"access_type": "offline"
}
然后创建一个 AccessToken 实例:
AccessToken token = new AccessToken(tokenValue, expiresIn);
但出现以下错误:
07-04 20:14:07.395 3023-3067/com.google.cloud.android.speech E/SpeechService: Error calling the API.
io.grpc.StatusRuntimeException: UNKNOWN
at io.grpc.Status.asRuntimeException(Status.java:543)
at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:395)
at io.grpc.ClientInterceptors$CheckedForwardingClientCall.start(ClientInterceptors.java:202)
at io.grpc.stub.ClientCalls.startCall(ClientCalls.java:276)
at io.grpc.stub.ClientCalls.asyncStreamingRequestCall(ClientCalls.java:266)
at io.grpc.stub.ClientCalls.asyncBidiStreamingCall(ClientCalls.java:106)
at com.google.cloud.speech.v1.SpeechGrpc$SpeechStub.streamingRecognize(SpeechGrpc.java:217)
at com.google.cloud.android.speech.SpeechService.startRecognizing(SpeechService.java:264)
at com.google.cloud.android.speech.MainActivity$1.onVoiceStart(MainActivity.java:62)
at com.google.cloud.android.speech.VoiceRecorder$ProcessVoice.run(VoiceRecorder.java:199)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: OAuth2Credentials instance does not support refreshing the access token. An instance with a new access token should be used, or a derived type that supports refreshing.
at com.google.auth.oauth2.OAuth2Credentials.refreshAccessToken(OAuth2Credentials.java:208)
at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:175)
at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:161)
at com.google.cloud.android.speech.SpeechService$GoogleCredentialsInterceptor.getRequestMetadata(SpeechService.java:532)
at com.google.cloud.android.speech.SpeechService$GoogleCredentialsInterceptor.access$900(SpeechService.java:450)
at com.google.cloud.android.speech.SpeechService$GoogleCredentialsInterceptor$1.checkedStart(SpeechService.java:474)
at io.grpc.ClientInterceptors$CheckedForwardingClientCall.start(ClientInterceptors.java:194)
at io.grpc.stub.ClientCalls.startCall(ClientCalls.java:276)
at io.grpc.stub.ClientCalls.asyncStreamingRequestCall(ClientCalls.java:266)
at io.grpc.stub.ClientCalls.asyncBidiStreamingCall(ClientCalls.java:106)
at com.google.cloud.speech.v1.SpeechGrpc$SpeechStub.streamingRecognize(SpeechGrpc.java:217)
at com.google.cloud.android.speech.SpeechService.startRecognizing(SpeechService.java:264)
at com.google.cloud.android.speech.MainActivity$1.onVoiceStart(MainActivity.java:62)
at com.google.cloud.android.speech.VoiceRecorder$ProcessVoice.run(VoiceRecorder.java:199)
at java.lang.Thread.run(Thread.java:764)
我可以使用示例代码:
final InputStream stream = getResources().openRawResource(R.raw.credential);
final GoogleCredentials credentials =
GoogleCredentials.fromStream(stream).createScoped(SCOPE);
final AccessToken token = credentials.refreshAccessToken();
得到一个working Token,通过getTokenValue()得到它的token值,通过getExpirationTime得到它的过期时间。然后创建一个 Token 实例:
AccessToken token = new AccessToken(tokenValue, expiresIn);
返回一个 AccessToken 实例。但它会导致同样的错误。
所以我的问题是,当我获得令牌值字符串和到期时间时,如何创建一个 工作 AccessToken 实例。
【问题讨论】:
-
确保您为 expirationTime 参数设置了有效的时间戳。如果时间戳是在服务器端生成的,则应使用正确的时区生成。
标签: android google-cloud-platform google-authentication google-speech-api