【发布时间】:2016-11-30 15:05:15
【问题描述】:
继续使用 Cloud Speech API 进行测试... 我从here 下载了另一个示例,并且我稍微修改了它,因为不推荐使用 ClientAuthInterceptor 类。我正在使用服务帐户,相关使用的代码是这样的:
// This is contained in the MainActivity code.
String fCred = _lu.getBasePath() + "auth.json";
try
{
InputStream isCred = new FileInputStream(fCred);
ManagedChannel channel = ManagedChannelBuilder.forAddress(HOSTNAME, PORT).build();
mStreamingClient = new StreamingRecognizeClient(channel, isCred, RECORDER_SAMPLERATE);
}
catch (IOException openEx)
{
Log.e(MainActivity.class.getSimpleName(), "Error", openEx);
}
// This is contained in the StreamingRecognizeClient class code
public StreamingRecognizeClient(ManagedChannel channel, InputStream credentials, int samplingRate) throws IOException
{
this.mSamplingRate = samplingRate;
this.mChannel = channel;
GoogleCredentials creds = GoogleCredentials.fromStream(credentials);
if (creds.createScopedRequired())
creds = creds.createScoped(OAUTH2_SCOPES);
CallCredentials callCreds = MoreCallCredentials.from(creds);
mSpeechClient = SpeechGrpc.newStub(channel).withCallCredentials(callCreds);
credentials.close();
}
所有这些代码在应用程序初始化期间被调用,文件auth.json是谷歌控制台生成的认证文件,包含项目ID和私钥的所有信息。 然后函数 recognizeBytes 被第一块音频数据调用;在发送实际音频数据之前,您必须发送仅包含识别配置的消息,这是通过此功能完成的:
private void initializeRecognition() throws InterruptedException, IOException
{
requestObserver = mSpeechClient.streamingRecognize(this);
RecognitionConfig config = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.FLAC).setSampleRate(mSamplingRate).build();
StreamingRecognitionConfig streamingConfig = StreamingRecognitionConfig.newBuilder().setConfig(config).setInterimResults(true).setSingleUtterance(true).build();
StreamingRecognizeRequest initial = StreamingRecognizeRequest.newBuilder().setStreamingConfig(streamingConfig).build();
requestObserver.onNext(initial);
}
在第一次调用之后,我得到的是 400 错误代码,如下面的堆栈跟踪所述:
11-30 15:34:12.916 3676-7309/? W/StreamingRecognizeClient: recognize failed: {0}: Status{code=UNAUTHENTICATED, description=null, cause=java.io.IOException: Error getting access token for service account: }
11-30 15:34:12.918 3676-7309/? E/StreamingRecognizeClient: Error to Recognize.
io.grpc.StatusRuntimeException: UNAUTHENTICATED
at me.yurifariasg.StreamingRecognizeClient.onError(StreamingRecognizeClient.java:98)
at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:395)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:481)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:398)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:513)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:52)
at io.grpc.internal.SerializingExecutor$TaskRunner.run(SerializingExecutor.java:154)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.io.IOException: Error getting access token for service account:
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:227)
at com.google.auth.oauth2.OAuth2Credentials.refresh(OAuth2Credentials.java:97)
at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:74)
at io.grpc.auth.GoogleAuthLibraryCallCredentials$1.run(GoogleAuthLibraryCallCredentials.java:113)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request
{
"error" : "invalid_grant",
"error_description" : "Invalid JWT Signature."
}
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1054)
at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:225)
... 6 more
由于签署请求的所有过程都是由谷歌库执行的,我不知道具体可以做什么...... 我已经向 Google 技术支持发送了一个请求,在试用期内可用,但也许有人有一些建议。
谢谢。 鲁道夫。
【问题讨论】:
-
我的一个朋友(我为他发布了这个问题)终于创建了一个 WORKING 样本,可以发送一个预先录制的 flac 音频文件。如果有人有兴趣,我会询问工作代码。这是 Android Studio 中的一个小项目。
标签: android oauth2 service-accounts