How to check if the Google client API key is valid in the Google PHP API library?

I applied the Google API client correctly in my PHP application. I was able to connect to the service I wanted.

But now I want to check whether the API key entered by the user is really entered.

I looked around or at the methods exposed by the Google_Client () class, but I don't think I'm sure how to check this.

The following is the method in my class that the client creates:

private function client( $api_key ) { $client = new \Google_Client(); $client->setClassConfig( 'Google_Cache_File', 'directory', $this->cache_dir ); $client->setDeveloperKey( $api_key ); $client->setApplicationName( $this->name ); $client->setScopes( array( \Google_Service_Calendar::CALENDAR_READONLY ) ); $client->setAccessType( 'online' ); return $client; } 

And I want to do another way to find out if the API key is really used or not ...

  public function validate_api_key( $api_key ) { $client = $this->client( $api_key ); // What should I use here to check if $api_key is valid? if ( $client ) { return true; } return 'error'; } 

Or do I need to connect to the service and then check if I have read access? But I believe that there is a simpler and better way to do this ...

+6
source share
1 answer

you can check using javascript; an invalid key always sets the undocumented property window.G_INCOMPAT . you might need to clear monkeys with function alert() {} to get rid of alert() for an invalid key, which would only interfere with the key installation process.

this answer has an interesting approach to alert()

there is also a documented feature that you can connect to: https://developers.google.com/maps/documentation/javascript/events#auth-errors

if it really needs to be PHP, you can still create JS and run it using PhantomJS.

see my github: php-phantomjs ... this is not impossible.

+1
source

All Articles