我正在通过使用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();
并获得一个有效令牌,并使用getTokenValue()
获得其令牌值,并通过getExpirationTime
获得其到期时间。然后创建一个令牌实例:
AccessToken token = new AccessToken(tokenValue, expiresIn);
返回一个AccessToken实例。但这会导致相同的错误。
所以我的问题是,当我获得令牌值字符串和到期时间时,如何创建一个工作 AccessToken实例。
答案 0 :(得分:0)
不幸的是,我仍然无法通过构造函数创建有效的AccessToken
。但是我使用套接字服务器来解决问题。因为AccessToken
已经实现了Serializable
接口。因此,在客户端和服务器之间进行通信要容易得多。
服务器
private void runServer() throws IOException, ClassNotFoundException{
ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();
ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
final InputStream stream = new ByteArrayInputStream(credential.getBytes(StandardCharsets.UTF_8));
final GoogleCredentials credentials = GoogleCredentials.fromStream(stream)
.createScoped(SCOPE);
final AccessToken token = credentials.refreshAccessToken();
os.writeObject(token);
}