Google Maps Android API: Authorization failure - android-studio-3.0

Android Studio 3.6
I added the following in my app/build.gradle file:
implementation 'com.google.android.gms:play-services-maps:17.0.0'
In google_maps_api.xml, I've added my API key like this:
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyAu_1111111</string>
In manifest file:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAu_1111111" />
On google console my API key is ENABLED
Here's my layout:
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/profileDetailsToolbar"
tools:context=".ui.activity.MapActivity" />
Here's my actvity:
class MapActivity : RootActivity(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dataBinding =
DataBindingUtil.setContentView(this, R.layout.map_activity)
dataBinding.setHandler(this)
init()
}
private fun init() {
setSupportActionBar(findViewById(R.id.toolBar))
getSupportActionBar()?.setDisplayHomeAsUpEnabled(true)
getSupportActionBar()?.setHomeButtonEnabled(true)
getSupportActionBar()?.setDisplayShowTitleEnabled(false)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Add a marker in Sydney and move the camera
val sydney = LatLng(-34.0, 151.0)
mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
}
}
But when I start my activity the map is not show.
In logcat I get the next error:
12-20 09:59:10.675 E/Google Maps Android API( 5153): Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.
12-20 09:59:10.676 E/Google Maps Android API( 5153): In the Google Developer Console (https://console.developers.google.com)
12-20 09:59:10.676 E/Google Maps Android API( 5153): Ensure that the "Google Maps Android API v2" is enabled.
12-20 09:59:10.676 E/Google Maps Android API( 5153): Ensure that the following Android Key exists:
12-20 09:59:10.676 E/Google Maps Android API( 5153): API Key: YOUR_API_KEY
12-20 09:59:10.676 E/Google Maps Android API( 5153): Android Application (<cert_fingerprint>;<package_name>): some_hex_value;com.myproject

Since you are already using an API key, the error could be caused by any of the following:
using an API key but from a project without a valid billing account
using an invalid/incorrect/malformed API key
using an API key which is restricted to other applications aside from your own
Maps SDK for Android is not enabled on your project
Make sure you are using an API key from a project that has a valid billing account since it is stated in Maps SDK for Android documentation that before you can start using the Google Maps Platform APIs and SDKs, you must sign up and create a billing account.
Also, if your API key has restrictions, make sure that it is properly restricted according to the API key best Practices. And lastly, make sure that you have enabled Maps SDK for Android for your maps project in the GCP console.

Related

Google OAuth in Desktop Mac App - Error 400: redirect_uri_mismatch

I am trying to authenticate to Google OAuth 2 via a native Mac app. I set up my app in the Google Developer Console as a Desktop application, and there is nowhere to specify a redirect_uri. As I understand it, those are only available to web applications.
But in composing my request in Swift using ASWebAuthenticationSession, my resulting authentication URL is like this (line breaks added for legibility):
https://accounts.google.com/o/oauth2/v2/auth?
client_id=******.apps.googleusercontent.com&
response_type=code&
redirect_uri=pro.cova%3A%2F&
scope=profile%2520email%2520https%3A%2F%2Fmail.google.com%2F&flowName=GeneralOAuthFlow
My app's custom URL type is pro.cova:/.
I'm getting this error from Google:
Error 400: redirect_uri_mismatch
What mismatch could be happening if there isn't one specified in the Google Developer Console?
It turns out that when building a native Mac app, you have to create "iOS" API credentials in the Google Developer Console. When setting it up, you have to specify your bundle ID like this: pro.cova
When you set the redirect_uri in your ASWebAuthenticationSession request, it must have a :/ at the end like this: redirect_uri=pro.cova:/
But the callbackURLScheme in your ASWebAuthenticationSession is without the :/ so it's just pro.cova.
I wish Google's API docs said "iOS or Mac" for the API credential options.

Google api php : Wrong audience in jwt for iOS only

I'm new and only developpeur in my company and they tasked my with clearing some bugs with their mobile app.
I'm working with an Ionic app and we have google login option for our users, using cordova plugin "cordova-plugin-googleplus".
The problem I'm having is the user tokenId I get from google doesn't work when I send it to my API using "google-api-php-client" library v1.1.5 :
- When the tokenId comes from an Android device, the connection works, the azp and aud in the token have a different value, one is the Android client ID and the other the web client id.
- When the tokenId comes from an iOS device, the connection doesn't work, the azp and aud both have the iOS client id value.
So in iOS case when google api php library checks the tokenId against the client id in my config it fails as the config give the web client id.
I have a couple questions, is it normal that the tokenId for iOS doesn't have the web client ID as the Android one ? And is possible in the config to allow more than one client ID ? (So I could allow both the web client ID and the iOS ID)
Thanks for any advice !
I found out the answer
Apparently for iOS if you don't add the option 'offline' : 'true' when calling the google login it doesn't use the webClientID you specified.
Not sure why it does that for iOS and not for Android though
It's in GooglePlus.m :
NSString* serverClientId = options[#"webClientID"];
...
if (serverClientId != nil && offline) {
signIn.serverClientID = serverClientId;
}
I'm still interested into why the different behavior for iOS and Android or if it's a bug, but hopefully this will help someone else someday

Oauth2 process with Google on Ionic 4 is stuck in "please wait"

I have a tutorial Ionic 4 application pasted from github.com/ionicthemes/ionic-google-login I have the following code that gets called:
this.googlePlus.login({
'scopes': '', // optional - space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
'webClientId': environment.googleWebClientId, // optional - clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
'offline': true, // Optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server
}).then(user => {
console.log('+++ user is:', user);
}, err => {
console.log("+++ google login error:", err);
})
And neither success, nor error messages get printed. I am stick waiting for a response from Google.
I did this: keytool -exportcert -keystore path-to-debug-or-production-keystore -list -v and added the sha1 fingerprint on console.developers.google.com. The package name matches (com.tgm_ionic) and I selected oauth2 android flow.
Extra info: This used to work on the browser platform, but would fail on android simulator with error 10, which is "invalid audience." So I created new credentials to move from browser platform to android platform (still oauth2), and now I encounter this issue. The goal here is to be able to login on android, not in the browser.
Verify that your webClientId is correct. You can reference this in the Google APIs console, then look under credentials and check your OAuth 2.0 client IDs. Often times with these hybrid apps folks are using the android key when the web client ID is needed.

Stubbing Google Maps/Places iOS SDK calls for unit testing

I'm trying to stub my Google Places calls using a local json file. Using Charles proxy, I find that the Google Maps SDK uses the url https://www.googleapis.com. So, I try to stub like:
stub(isHost("https://www.googleapis.com")) { request in
OHHTTPStubsResponse(fileAtPath: "google_maps.json", statusCode: 200, headers: ["Content-Type":"application/json"])
}
But it's not intercepting the google maps SDK calls. Any ideas? Could this be the Google Maps SDK using something other than Cocoa's URL Loading System?
Answer given here: (I was using the incorrect host name, should use simply "www.googleapis.com")
https://github.com/AliSoftware/OHHTTPStubs/issues/219

Request Denied in Google Maps geocode API iOS

I am trying to integrate google maps in my iOS App. I created and tried the iOS key, Server key and browser key as well. I regenerated the keys, but still gets the status as "ACCESS DENIED". I have seen many answers here, but none of the solutions work for me. I get the following response
This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console
and sometimes this
"error_message" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address xxx.xxx.xxx.xxx, with empty referer"
And I tried to use the regenerated key, and i get the following result.
"error_message" : "The provided API key is expired."
The solution may be simple, but I'm really stuck. Any help is appreciated
the log or error is clear.
for use google_maps you must :
• register your app bundle in Google Developer
• get api key ( if you had get it before update it)
• call the google initialize in AppDelegate
[GMSServices provideAPIKey:GOOGLE_MAP_SERVICE];

Resources