Closed Dartui closed 8 years ago
Yes you can and I have used this in the past however it is unrelated to this project. Please refer to the official google php client sdk.
For everyone who is looking for how to get access token for service by .json file:
include_once 'vendor/autoload.php';
// required for checkServiceAccountCredentialsFile()
include_once "vendor/google/apiclient/examples/templates/base.php";
$client = new Google_Client();
if ($credentials_file = checkServiceAccountCredentialsFile()) {
$client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
// this will load service json file, which should be named service-account-credentials.json
// in my application it is in vendor/google/apiclient folder
$client->useApplicationDefaultCredentials();
} else {
echo missingServiceAccountDetailsWarning();
exit;
}
$token = $client->fetchAccessTokenWithAssertion();
// print_r of $token
// Array ( [access_token] => ACCESS_TOKEN [token_type] => Bearer [expires_in] => 3600 )
Is there any way to get service account access token via .json file? I am using this example (https://github.com/google/google-api-php-client/blob/master/examples/service-account.php) to connect with OAuth, but I cannot get in any way token. My code:
Here is class inside $client:
I am using latest version of OAuth and that's why I cannot use autorization with .p12 file because of this: