【问题标题】:Google Oauth2 with Service account for Google Speech API rpc not working带有 Google Speech API rpc 服务帐户的 Google Oauth2 无法正常工作
【发布时间】:2017-07-23 11:12:32
【问题描述】:

我正在开发一个 Android 应用程序,该应用程序目前通过 Google 云流 Speech API 使用 Google Speech to text,但是我无法让身份验证正常工作。

我想在 Google Cloud API 上使用 OAuth2 和来自 Google 服务帐户的 authkey.json 进行身份验证。

我使用包google.cloud.speech.v1beta1 中的SpeechGrpc.SpeechStub 类来调用RPC 方法。这需要在构造函数中使用ManagedChannel,通过它与 API 进行通信。

我在这里找到了一个示例,展示了如何使用GoogleCrendential 创建ManagedChannelGoogleCloudPlatform/java-docs-samples

它一直有效,直到将凭据截获到通道中为止。 ClientAuthInterceptor 类,在示例中用于拦截进入通道的凭据是

  1. 已弃用
  2. 仅将com.google.auth.Credentials 作为参数,而GoogleCredential 继承自com.google.api.client.auth.oauth2.Credential

下一次尝试:谷歌示例:Using OAuth 2.0 with the Google API Client Library for Java

它建议我使用Plus 来包装GoogleCrendential。 但Plus 也是deprecated 甚至不包含 一种方法

builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)

如该页面上的主题 GoogleCredential 所示,也不是方法

Plus.Builder(httpTransport, jsonFactory, credential)
    .setApplicationName(APPLICATION_NAME).build();

如该页面上的 ServiceAccounts 部分所示。

我用完了选项,因为用于调用 rpc 方法的 SpeechGrpc.SpeechStub 类需要 ManagedChannel 才能工作。

另一种选择可能是将凭据添加到SpeechGrpc.SpeechStub,而不是通过该方法添加到ManagedChannel

SpeechGrpc.newStub(channel).withCallCredentials(CallCredentials creds)

如果可能,我想知道如何从服务帐户的 authkey.json 文件中创建 CallCredentials

我阅读了过去几周内我能找到的关于这个主题的所有示例、谷歌文档和主题,但我没有让这个身份验证起作用。

我希望现在这里的任何人都可以帮助我解决这个令人困惑的 google api 和库问题。

【问题讨论】:

  • 当这是一个公共 api 并且您可以只使用 api 密钥时,您想要使用服务帐户和 oauth 的任何特殊原因?
  • 好吧,api 是公开的,是的,但是对于流媒体用例,我用我的帐户付款。在所有文档中都被告知只能通过 oauth ( cloud.google.com/speech/reference/rpc/…> ) 使用 api。对我来说,我使用哪种身份验证并不重要。你有 api key 方法的工作解决方案吗?
  • API 密钥将识别您的帐户。 Oauth 用于访问私有数据。

标签: java android google-api google-api-java-client google-speech-api


【解决方案1】:

您好,您只需要在代码开始前添加一行。在 google-doc-sample 案例中,将其添加到 main() 方法的第一行。

GoogleCredentials.fromStream(new FileInputStream());

另外,请参阅此链接。 https://github.com/google/google-auth-library-java

在 0.6.0 版本之后,谷歌将他们的身份验证移到了以下位置:

google-auth-library-oauth2-http

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多