如何设置环境变量GOOGLE_APPLICATION_CREDENTIALS?

时间:2018-04-25 10:18:44

标签: android google-cloud-platform dialogflow

我知道之前已经问过这种类型的问题而且我已经完成了这个但仍然面临这个问题。我正在使用this example。但是当我运行我的代码时,我得到了以下异常

  

无法检测我们是否在Google Compute Engine上运行。                              java.net.ConnectException:无法连接到/169.254.169.254:80                                  在com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)                                  在com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)                                  在com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)                                  在com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)                                  在com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)                                  在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)                                  在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)                                  在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)                                  在com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)                                  在com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:104)                                  在com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)                                  在com.google.auth.oauth2.ComputeEngineCredentials.runningOnComputeEngine(ComputeEngineCredentials.java:191)                                  在com.google.auth.oauth2.DefaultCredentialsProvider.tryGetComputeCredentials(DefaultCredentialsProvider.java:270)                                  在com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentialsUnsynchronized(DefaultCredentialsProvider.java:194)                                  在com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:112)                                  在com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:127)                                  在com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:100)                                  在com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:53)                                  在com.google.api.gax.rpc.ClientContext.create(ClientContext.java:134)                                  在com.google.cloud.dialogflow.v2.stub.GrpcSessionsStub.create(GrpcSessionsStub.java:75)                                  在com.google.cloud.dialogflow.v2.stub.SessionsStubSettings.createStub(SessionsStubSettings.java:100)                                  在com.google.cloud.dialogflow.v2.SessionsClient。(SessionsClient.java:132)                                  在com.google.cloud.dialogflow.v2.SessionsClient.create(SessionsClient.java:114)                                  在com.google.cloud.dialogflow.v2.SessionsClient.create(SessionsClient.java:106)                                  at com.example.lpt_0096.dialogflowv2.MainActivity $ MyAsync.detectIntentStream(MainActivity.java:76)                                  at com.example.lpt_0096.dialogflowv2.MainActivity $ MyAsync.doInBackground(MainActivity.java:61)                                  在android.os.AsyncTask $ 2.call(AsyncTask.java:333)                                  at java.util.concurrent.FutureTask.run(FutureTask.java:266)                                  在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245)                                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)                                  at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)                                  在java.lang.Thread.run(Thread.java:764)   W / System.err:java.io.IOException:应用程序默认凭据不可用。如果在Google Compute Engine中运行,则可以使用它们。否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向定义凭证的文件。有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials。   W / System.err:at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:119)   W / System.err:at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:127)   W / System.err:at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:100)   W / System.err:at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:53)   W / System.err:at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:134)   W / System.err:at com.google.cloud.dialogflow.v2.stub.GrpcSessionsStub.create(GrpcSessionsStub.java:75)   W / System.err:at com.google.cloud.dialogflow.v2.stub.SessionsStubSettings.createStub(SessionsStubSettings.java:100)   W / System.err:在com.google.cloud.dialogflow.v2.SessionsClient。(SessionsClient.java:132)   W / System.err:at com.google.cloud.dialogflow.v2.SessionsClient.create(SessionsClient.java:114)   W / System.err:在com.google.cloud.dialogflow.v2.SessionsClient.create(SessionsClient.java:106)   W / System.err:at com.example.lpt_0096.dialogflowv2.MainActivity $ MyAsync.detectIntentStream(MainActivity.java:76)   W / System.err:at com.example.lpt_0096.dialogflowv2.MainActivity $ MyAsync.doInBackground(MainActivity.java:61)   W / System.err:在android.os.AsyncTask $ 2.call(AsyncTask.java:333)   W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:266)   W / System.err:在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245)   W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)   W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:636)   W / System.err:at java.lang.Thread.run(Thread.java:764)

从哪里可以在android studio中设置环境变量,我是windows用户

1 个答案:

答案 0 :(得分:2)

从错误

  

java.io.IOException:Application Default Credentials不是   可用。如果在Google Compute Engine中运行,则可以使用它们。   否则,环境变量GOOGLE_APPLICATION_CREDENTIALS   必须定义指向定义凭证的文件

     

请参阅   https://developers.google.com/accounts/docs/application-default-credentials   了解更多信息。

您可能需要在环境变量路径GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service_accont_key_file.json中添加它。并且应该从这里获取json文件https://cloud.google.com/docs/authentication/getting-started。此外,如果您不知道如何设置环境变量路径,可以查看此https://www.computerhope.com/issues/ch000549.htm