[英]Google Oauth2 with Service account for Google Speech API rpc not working
I am developing an android application, which uses Google Speech to text via the Google cloud streaming Speech API at the moment, however I am having trouble to get the authentication to work. 我正在开发一个Android应用程序,该应用程序目前使用Google语音通过Google云流式语音API通过文本发送文本,但是我无法使身份验证正常工作。
I want to authenticate at the Google Cloud API using OAuth2 and a authkey.json from a Google Service account. 我想使用OAuth2和来自Google服务帐户的authkey.json在Google Cloud API上进行身份验证。
I use the SpeechGrpc.SpeechStub
class from package google.cloud.speech.v1beta1
to call RPC methods. 我使用包
google.cloud.speech.v1beta1
的SpeechGrpc.SpeechStub
类来调用RPC方法。 This needs a ManagedChannel
in the constructor through which it communicates with the API. 这需要在构造函数中通过
ManagedChannel
与API通信。
I found an example showing how to create a ManagedChannel
with GoogleCrendential
here: GoogleCloudPlatform/java-docs-samples 我在这里找到了一个示例,展示了如何使用
GoogleCrendential
创建ManagedChannel
: GoogleCloudPlatform / java-docs-samples
It worked until the point of intercepting the credentials into the channel. 它一直有效,直到将凭据拦截到通道中为止。 The
ClientAuthInterceptor
class, which is used in the example to intercept the credentials into the channel is 在示例中用于将凭据拦截到通道中的
ClientAuthInterceptor
类是
com.google.auth.Credentials
as argument while GoogleCredential
inherits from com.google.api.client.auth.oauth2.Credential
. GoogleCredential
继承自com.google.api.client.auth.oauth2.Credential
而仅接受com.google.auth.Credentials
作为参数。 Next try: google example: Using OAuth 2.0 with the Google API Client Library for Java 下一个尝试:google示例: 将OAuth 2.0与Java的Google API客户端库一起使用
It advises me to use a Plus
to wrap the GoogleCrendential
. 它建议我使用
Plus
来包装GoogleCrendential
。 But the Plus
is also deprecated
and does not even contain either a method 但是
Plus
也deprecated
,甚至都不包含任何一种方法
builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
as shown in topic GoogleCredential on that page, nor a method 如该页面上的主题GoogleCredential所示,也没有方法
Plus.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(APPLICATION_NAME).build();
As shown in section ServiceAccounts on that page. 如该页面上的ServiceAccounts部分中所示。
I am running out of options as the SpeechGrpc.SpeechStub
class, which is used to call rpc methods, needs a ManagedChannel
to work. 我用完了所有选项,因为用来调用rpc方法的
SpeechGrpc.SpeechStub
类需要ManagedChannel
才能工作。
An other option could maybe be to add the credentials to the SpeechGrpc.SpeechStub
and not to the ManagedChannel
through the method 另一个选择可能是将凭据添加到
SpeechGrpc.SpeechStub
而不是通过方法添加到ManagedChannel
SpeechGrpc.newStub(channel).withCallCredentials(CallCredentials creds)
If this is possible, I would like to know, how I can create CallCredentials
out of a Service Account's authkey.json
file. 如果可能,我想知道如何从服务帐户的
authkey.json
文件中创建CallCredentials
。
I read through every example, google documentation and thread on this topic I could find within the last weeks, but I don't get this authentication to work. 我在过去几周内通读了有关该主题的所有示例,google文档和主题,但是我无法通过此身份验证。
I hope anyone in here now can help me out of this confusing google api and library issue. 我希望现在这里的任何人都可以帮助我解决这个令人困惑的Google api和库问题。
Hi you just need to add one line before your code starts. 嗨,您只需要在代码开始前添加一行即可。 In the google-doc-sample case add it in main() method very first line.
在google-doc-sample情况下,将其添加到main()方法的第一行。
GoogleCredentials.fromStream(new FileInputStream()); GoogleCredentials.fromStream(new FileInputStream());
Also, refer this link. 另外,请参考此链接。 https://github.com/google/google-auth-library-java
https://github.com/google/google-auth-library-java
After 0.6.0 release google moved their authentication under this: 在0.6.0版本发布后,google将其身份验证移至以下位置:
google-auth-library-oauth2-http 谷歌认证 - 库的oauth2-HTTP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.