Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically access the Google API key in google-services.json

The app uses Google Cloud Messaging and contains a google-services.json file which itself contains Google API authentication info, including the API key. GCM functions as expected.

The app contains a testing facility that sends a GCM message using HTTP, and it needs the API key. How do I access that key, the one stored in google-services.json? I do not want to use GoogleCloudMessaging to send the messages (I'm using OkHttp and it does the job nicely).

Hopefully it is not necessary to duplicate it in the app.

like image 336
Ollie C Avatar asked May 16 '16 11:05

Ollie C


People also ask

How do I access my Google API key?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key.

What is API key in Google services json?

An API key is a unique string that's used to route requests to your Firebase project when interacting with Firebase and Google services. This page describes basic information about API keys as well as best practices for using and managing API keys with Firebase apps.

How do I download Google API Key as json?

Click the Keys tab. Click the Add key drop-down menu, then select Create new key. Select JSON as the Key type and click Create.


1 Answers

Sorry, a bit late to the game here...

The google-services.json file gets parsed and its values get added to an xml which you have access to: https://developers.google.com/android/guides/google-services-plugin

In your case, you could get the api key with: activity.getResources().getString(R.string.google_api_key);

like image 190
marc_ferna Avatar answered Oct 19 '22 21:10

marc_ferna