( ADC : https://cloud.google.com/docs/authentication/provide-credentials-adc#how-to )
I want to use Application Default Credentials (ADC) in Android Studio,
No,1. I created a project in google cloud console.
No,2. I installed google CLI, and I entered the ‘gcloud auth application-default login
‘ command in the CLI.
No,3. The Google login window appears, so I’m logged in.
The project selection came up, so I chose the project I created in No,1.
No,4. JSON key was stored on my local computer.
No,5.In the environment variable setting, ‘GOOGLE_APPLICATION_CREDENTIALS’ – ‘JSON FILE LOCATION’ was set.
No,6. In Android Studio, run the code below. ( Kotlin )
@Throws(IOException::class)
fun test() {
val routesSettings = RoutesSettings.newBuilder()
.setHeaderProvider {
val headers: MutableMap<String, String> =
HashMap()
headers["X-Goog-FieldMask"] = "*"
headers
}.build()
val routesClient = RoutesClient.create(routesSettings)
val response = routesClient.computeRoutes(
ComputeRoutesRequest.newBuilder()
.setOrigin(Waypoint.newBuilder().setPlaceId("ChIJeRp....").build())
.setDestination(
Waypoint.newBuilder().setPlaceId("ChIJG3kh....").build()
)
.setRoutingPreference(RoutingPreference.TRAFFIC_AWARE)
.setTravelMode(RouteTravelMode.DRIVE).build()
)
println("Response: $response")
}
No,7. I encountered the following error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grpc_example/com.example.grpc_example.MainActivity}: java.io.IOException: Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.io.IOException: Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:127)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:152)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:124)
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:70)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:162)
at com.google.maps.routing.v2.stub.GrpcRoutesStub.create(GrpcRoutesStub.java:75)
at com.google.maps.routing.v2.stub.RoutesStubSettings.createStub(RoutesStubSettings.java:112)
at com.google.maps.routing.v2.RoutesClient.<init>(RoutesClient.java:211)
at com.google.maps.routing.v2.RoutesClient.create(RoutesClient.java:194)
at com.example.grpc_example.MainActivity.test(MainActivity.kt:52)
at com.example.grpc_example.MainActivity.onCreate(MainActivity.kt:25)
at android.app.Activity.performCreate(Activity.java:8595)
at android.app.Activity.performCreate(Activity.java:8573)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
Q) Where did I set it up wrong?
Why won’t ADC certification work?
(The reason for using ADC authentication is to use the google route G-RPC function.)
Thank you.