簡體   English   中英

帶有Google Speech API RPC服務帳戶的Google Oauth2無法正常工作

[英]Google Oauth2 with Service account for Google Speech API rpc not working

我正在開發一個Android應用程序,該應用程序目前使用Google語音通過Google雲流式語音API通過文本發送文本,但是我無法使身份驗證正常工作。

我想使用OAuth2和來自Google服務帳戶的authkey.json在Google Cloud API上進行身份驗證。

我使用包google.cloud.speech.v1beta1SpeechGrpc.SpeechStub類來調用RPC方法。 這需要在構造函數中通過ManagedChannel與API通信。

我在這里找到了一個示例,展示了如何使用GoogleCrendential創建ManagedChannelGoogleCloudPlatform / java-docs-samples

它一直有效,直到將憑據攔截到通道中為止。 在示例中用於將憑據攔截到通道中的ClientAuthInterceptor類是

  1. 棄用
  2. GoogleCredential繼承自com.google.api.client.auth.oauth2.Credential而僅接受com.google.auth.Credentials作為參數。

下一個嘗試:google示例: 將OAuth 2.0與Java的Google API客戶端庫一起使用

它建議我使用Plus來包裝GoogleCrendential 但是Plusdeprecated ,甚至都不包含任何一種方法

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文檔和主題,但是我無法通過此身份驗證。

我希望現在這里的任何人都可以幫助我解決這個令人困惑的Google api和庫問題。

嗨,您只需要在代碼開始前添加一行即可。 在google-doc-sample情況下,將其添加到main()方法的第一行。

GoogleCredentials.fromStream(new FileInputStream());

另外,請參考此鏈接。 https://github.com/google/google-auth-library-java

在0.6.0版本發布后,google將其身份驗證移至以下位置:

谷歌認證 - 庫的oauth2-HTTP

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM