How can i set the environment variable GOOGLE_APPL

2019-07-28 20:06发布

问题:

I know this type of question has been asked before and i have gone through this but still facing this issue. I was using this example . But while i run my code i got following exception

Failed to detect whether we are running on Google Compute Engine. java.net.ConnectException: Failed to connect to /169.254.169.254:80 at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143) at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112) at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184) at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126) at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127) at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:104) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981) at com.google.auth.oauth2.ComputeEngineCredentials.runningOnComputeEngine(ComputeEngineCredentials.java:191) at com.google.auth.oauth2.DefaultCredentialsProvider.tryGetComputeCredentials(DefaultCredentialsProvider.java:270) at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentialsUnsynchronized(DefaultCredentialsProvider.java:194) at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:112) at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:127) at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:100) at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:53) at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:134) at com.google.cloud.dialogflow.v2.stub.GrpcSessionsStub.create(GrpcSessionsStub.java:75) at com.google.cloud.dialogflow.v2.stub.SessionsStubSettings.createStub(SessionsStubSettings.java:100) at com.google.cloud.dialogflow.v2.SessionsClient.(SessionsClient.java:132) at com.google.cloud.dialogflow.v2.SessionsClient.create(SessionsClient.java:114) at 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) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at 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) at java.lang.Thread.run(Thread.java:764) W/System.err: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. 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: at 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: at 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: at android.os.AsyncTask$2.call(AsyncTask.java:333) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266) W/System.err: at 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)

from where can i set environment variable in android studio, I am windows user

回答1:

From the error

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials

See https://developers.google.com/accounts/docs/application-default-credentials for more information.

You may need to add this in the environment variable path GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service_accont_key_file.json. And should get the json file from here https://cloud.google.com/docs/authentication/getting-started . Also if you don't know how to set environment variable path you can check this https://www.computerhope.com/issues/ch000549.htm