gpt4 book ai didi

android - 错误 : OAuth2Credentials instance does not support refreshing the access token. 如何使用 token 值字符串创建 AccessToken 实例?

转载 作者:行者123 更新时间:2023-12-05 00:06:41 25 4
gpt4 key购买 nike

我正在使用 Android 上的 Google Speech to Text 服务将语音流式传输到文本。 Here is official sample repository. 示例运行成功。但是或者,您应该在服务器端获取访问 token ,并将其提供给客户端应用。

所以我设置了一个服务器并为应用程序创建 RESTful api 以获取访问 token 。{"token":"1234567890sdertyuikjhgfghjk....."}

还可以使用 api: https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= {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 实例。但它会导致同样的错误。

所以我的问题是,当我获得 token 值字符串和到期时间时,如何创建一个工作 AccessToken 实例。

最佳答案

不幸的是,我仍然无法通过构造函数创建有效的AccessToken。但是我使用套接字服务器来解决问题。因为AccessToken已经实现了Serializable接口(interface)。因此在客户端和服务器之间进行通信要容易得多。

服务器

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);
}

关于android - 错误 : OAuth2Credentials instance does not support refreshing the access token. 如何使用 token 值字符串创建 AccessToken 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51196604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com