[英]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.v1beta1
的SpeechGrpc.SpeechStub
類來調用RPC方法。 這需要在構造函數中通過ManagedChannel
與API通信。
我在這里找到了一個示例,展示了如何使用GoogleCrendential
創建ManagedChannel
: GoogleCloudPlatform / java-docs-samples
它一直有效,直到將憑據攔截到通道中為止。 在示例中用於將憑據攔截到通道中的ClientAuthInterceptor
類是
GoogleCredential
繼承自com.google.api.client.auth.oauth2.Credential
而僅接受com.google.auth.Credentials
作為參數。 下一個嘗試:google示例: 將OAuth 2.0與Java的Google API客戶端庫一起使用
它建議我使用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文檔和主題,但是我無法通過此身份驗證。
我希望現在這里的任何人都可以幫助我解決這個令人困惑的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.