Validate google key api

Asked

Viewed 459 times

3

I am making an application, where I will have to validate a google maps API key. The problem is that I do not know what standards it follows:

AIzaSyA-OlPHUh2W7LUZXVjQjjxQ8fdCWdAASmc 

Here it is visible that they are capital letters and minuscules, numbers and dashes. But will always be so?

Someone knows the pattern of these keys, and how to do this with JS and PHP?

JS:

var inputstr="A3aa622-_";
var regex=/^[A-Za-z0-9-_]{30,50}$/g;
if (regex.test(inputstr)){
    alert("sim");
}else{
    alert("Nao");
}
  • I think the only way to validate them would be to perform a GET/POST for the API using such a key. This way the API should return whether the key is true or not.

  • I get it. At the moment I don’t care much if the api is true or false, I would like to validate more, so that the suit doesn’t add anything, something like that, would it help: regex=/^[A-Za-z0-9-_]{30,50}$/g;

1 answer

1

Assuming the key has exactly 39 Size characters; Consists of alphanumeric characters (letters and numbers), in any case (uppercase or lowercase); And may have dashes - and underlines _.

Follow the codes in JS and PHP able to make this pre-validation*:

Code in JavaScript:

function is_valid_key( key )
{
    if( key.length != 39 ) return false;
    var regx = new RegExp("^[A-Za-z0-9-_]+$");
    return regx.test(key);
}

Code in PHP:

function is_valid_key( $key )
{
    if( strlen( $key ) != 39 ) return false;
    return preg_match( '/^[a-zA-Z0-9-_]+$/', $key );
}
  • The @Inkeliz user is right, the only way to validate the key is through a GET/POST in the Google API, so the above code is just a pre-validation in order to avoid a overhead Unnecessary Gets and Network Posts.

I hope I’ve helped!

  • Detail, by the tests I performed, accepts _ (underline) ... ATT

  • 1

    @abcd: adjusted!

  • 1

    @Locobus Remembering that it is not possible to know if these values will really be kept for the keys, because nowhere have I found information about the number of characters and which characters can be used... ATT

Browser other questions tagged

You are not signed in. Login or sign up in order to post.