$client = new Client();
$client->setClientId(config('services.google.client_id'));
$client->setClientSecret(config('services.google.client_secret'));
$client->setRedirectUri(config('services.google.redirect'));
$client->setScopes(['https://www.googleapis.com/auth/calendar.readonly']);
$service = new \Google\Service\Calendar($client);
$item = new \Google\Service\Calendar\FreeBusyRequestItem();
$item->setId('<calendar_id');
$freebusy_req = new \Google\Service\Calendar\FreeBusyRequest();
$freebusy_req->setTimeMin(date(DateTime::ATOM, strtotime('2022/01/28')));
$freebusy_req->setTimeMax(date(DateTime::ATOM, strtotime('2022/01/29')));
$freebusy_req->setTimeZone('Europe/Madrid');
$freebusy_req->setItems([$item]);
$query = $service->freebusy
->query($freebusy_req);
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
My users have already gone through oauth and given permission to access their info. But I would like to asynchronously pull their calendar availablity.
However, running the above code gave me the JSON error. My knowledge of oauth is very limited. Any idea what I'm missing? Thank you.
Your users may have authorized your app but that code is doing nothing to tell the authorizing server that. All you are passing is a client id, in order to access user data for a user that has previously authorized your app you would need to pass a refresh token.
In the code below i have stored the users authorization in $tokenPath and I can then load that in.
function getClient()
{
$client = new Client();
$client->setApplicationName('Google calendarAPI PHP Quickstart');
$client->setScopes('https://www.googleapis.com/auth/calendar');
$client->setAuthConfig('C:\Development\FreeLance\GoogleSamples\Credentials\credentials.json');
$client->setAccessType('offline');
$client->setRedirectUri("http://127.0.0.1");
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($accessToken);
}
// If there is no previous token, or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}