I have correctly implemented a Google API client in my PHP application. I was able to connect to the service I wanted.
But now I want to check if an API key the user enters is valid or not.
I have looked around or at the methods exposed by Google_Client() class but I don't think I am sure how to check this.
Below here's the method in my class that creates a client:
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 make another method to tell if the API key used is valid 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 should I connect to a service and then check if I have read access from there? But I believe there is a simpler and better way to do this...