2017-03-21 12 views
0

テキストをある言語から別の言語に翻訳する必要があります。私はGoogle翻訳APIを使用しています。 しかし、認証を取得できません。私はコードを通じて認証情報を提供したい、私はグローバル変数で自動的に認証したくない(私はまだそれが良いアイデアだと思う)。認証できません:JSON入力が見つかりません

APIキーを設定する簡単な方法がありますが、簡単な方法で資格情報を指定する方法はありません。だから私は、サービスキーを作成するときに得られるJSONで認証することに決めました。

Scalaのコードで次に
{ 
"type": "service_account", 
"project_id": "toto", 
"private_key_id": "tata", 
"private_key": "titi", 
"client_email": "tete", 
"client_id": "tutu", 
"auth_uri": "https://accounts.google.com/o/oauth2/auth", 
"token_uri": "https://accounts.google.com/o/oauth2/token", 
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/toto" 
} 

、:

val cred = GoogleCredentials.fromStream(JSON_AUTHENTICATION_INPUT_STREAM) 
TranslateOptions.newBuilder().setCredentials(cred) 

そして、もちろん、私は次のエラーを取得する:だから

java.lang.IllegalArgumentException: no JSON input found 
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:125) 
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49) 
at com.google.api.client.json.JsonParser.startParsing(JsonParser.java:223) 
at com.google.api.client.json.JsonParser.parse(JsonParser.java:380) 
at com.google.api.client.json.JsonParser.parse(JsonParser.java:355) 
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87) 
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81) 
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.fromStream(GoogleCredential.java:250) 
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.fromStream(GoogleCredential.java:226) 
at services.TranslatorBaseImpl.<init>(Translator.scala:58) 

何? JSON認証が機能していませんか?コードでどのように認証できますか?

私は公式の文書で何時間も検索しましたが、そのような高度な事例では役に立たないです。あなたの答えのための

おかげで、 キス

答えて

0

は、より多くの調査の後、私はServiceAccountCredentials代わりのGoogleCredentialsを使用して成功しました。 しかし、たとえそれを動作させることができても、これは容認できません。 最初のものは2番目のサブクラスです。ドキュメントは明確に述べています:

static GoogleCredentials fromStream(InputStream credentialsStream)
-> Returns credentials defined by a JSON file stream.

これは動作しますか? 同じコード、同じ入力、同じconfですが、契約は満たされていません....

関連する問題