Last active
November 29, 2017 21:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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