$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"
}
我的用户已经通过了 oauth 并获得了访问其信息的权限。 但我想异步获取他们的日历可用性。
但是,运行上面的代码给了我 JSON 错误。我对oauth的了解非常有限。知道我缺少什么吗?谢谢你。
您的用户可能已经授权您的应用程序,但该代码没有执行任何操作来告诉授权服务器这一点。您传递的只是一个客户端 ID,为了访问之前已授权您的应用程序的用户的用户数据,您需要传递刷新令牌。
在下面的代码中,我已将用户授权存储在 $tokenPath 中,然后我可以将其加载。
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;
}