Google Maps API Key For Xamarin.Android App

If you want to use Google Maps in your Xamarin.Android app, you will need to obtain an API key. It’s easy to generate an API Key, but we will also want to restrict it’s usage, so that only your app can use it. This alleviates the security concern of placing the API Key in your app, and someone taking it for their own use.

Create Google Maps API Key

  1. Go to the Google Developers Console
  2. Click Create Project (If you don’t already have one, otherwise it defaults to an existing project). If you have an existing project, but want to create a new one, click the drop down, then the + button.
  3. Click ENABLE APIS AND SERVICES
  4. Click Google Maps Android API
  5. Then click enable
  6. Click Credentials, in the Menu, then click Create credentials and select API key

This is your API key that you can use in your Android app.

In your AndroidManifest.xml file, inside the application tag, place your API Key.

<application android:label="XamarinMaps.Android" android:icon="@drawable/icon"> 
    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR_API_KEY_HERE" /> 
</application>

Restrict Key Access

This API Key is now available, but can be used with anyone who has access to it. If you are using it Server side, then the security issues aren’t too great. However, if you need to distribute this with your app, then it is best to restrict this, so only your app can use it.

Generate Fingerprint

We first need to generate a fingerprint. This is done via the command or terminal line. And is in this format.

keytool -list -v -keystore [STORE FILENAME] -alias [ALIAS NAME] -storepass [STORE PASSWORD] -keypass [KEY PASSWORD]

For windows, you will find the debug values for your keystore here, and the default path for the Java SDK. You will need to go to this path, in the commandline to run the keytool.

  • [STORE FILENAME] = C:\Users\[USERNAME]\AppData\Local\Xamarin\Mono for Android\debug.keystore
  • C:\Program Files (x86)\Java\jdk[VERSIONNUMBER]\bin\

For a Mac, they will be as follows.

  • [STORE FILENAME] = /Users/[USERNAME]/.local/share/Xamarin/Mono for Android/debug.keystore
  • /System/Library/Java/JavaVirtualMachines/[VERSIONNUMBER].jdk/Contents/Home/bin/keytool

The debug values you will also want are

  1. [ALIAS NAME] = androiddebugkey
  2. [STORE PASSWORD] = android
  3. [KEY PASSWORD] = android

Here is an example, for myself in Windows.

C:\Program Files (x86)\Java\jdk1.8.0_112\bin>keytool -list -v -keystore "C:\Users\Adam\AppData\Local\Xamarin\Mono for Android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

The example output, you will get is.

Alias name: androiddebugkey
Creation date: 22/06/2017
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 6b038328
Valid from: Thu Jun 22 15:28:21 AEST 2017 until: Sat Jun 15 15:28:21 AEST 2047
Certificate fingerprints:
MD5: CF:F5:C2:46:91:E9:18:B2:63:12:57:F5:77:3F:ED:32
SHA1: E0:08:06:CD:89:68:BC:A8:81:F7:87:B6:ED:C4:B0:69:BF:01:87:96
SHA256: A2:FA:88:B3:3F:F7:51:93:5F:8F:8F:6F:C5:7C:1E:2A:2A:5F:5A:B6:00:21:B8:2A:7A:1E:6E:FC:D6:4D:26:E5
Signature algorithm name: SHA256withRSA
Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 28 0D 0F 34 33 28 18 9A 67 A8 B7 88 17 B8 A5 FB (..43(..c.......
0010: B7 FD 14 D6 ....
]
]

What you want is the SHA-1 signature.

SHA1: E0:08:06:CD:89:68:BC:A8:81:F7:87:B6:ED:C4:B0:69:BF:01:87:96

Add App Restriction

Now, lets go back to the developer console.

  1. Edit the API Key.
  2. Add the fingerprint, and your Xamarin.Android apps package name, and save.

Please note, that this has shown you how to do it, for a debug version. Create a new Fingerprint, against your production Keystore, before you deploy this to the PlayStore.

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer |
Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia


XAMARIN.FORMS MONTHLY NEWSLETTER

JOIN 1,000+ SUBSCRIBERS

  1. Don't miss out on updates
  2. The latest info from this site, Xamarin and the community
  3. Unsubscribe at any time*

* We use MailChimp, with double opt-in, and instant unsubscribe

Related Posts

Tagged:Tags:

Leave A Comment?