Skip to content

Instantly share code, notes, and snippets.

@TiagoSilvaPereira
Last active November 29, 2017 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TiagoSilvaPereira/a243c11d59a70a00a73dce356d637afc to your computer and use it in GitHub Desktop.
Save TiagoSilvaPereira/a243c11d59a70a00a73dce356d637afc to your computer and use it in GitHub Desktop.
Get an access_token with the Google API Client to authenticate your PHP Backend with google services, like Firebase, Cloud Storage, etc
public function getServiceAccountAccessToken() {
// put the serviceAccount credentials in the env variable
putenv('GOOGLE_APPLICATION_CREDENTIALS=../resources/json/serviceAccount.json');
// Add the scopes (here are the scopes to Firebase Auth)
$scopes = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database"
];
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scopes);
// Get the token
$fetchedToken = $client->fetchAccessTokenWithAssertion();
if(!isset($fetchedToken['access_token'])) {
throw new \Exception("Error obtaining the access_token", 1);
}
return $fetchedToken['access_token'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment