我找到了一个解决方案,我认为这是您想做的事情的“官方”。
首先,您必须激活 Google API“已安装应用程序的客户端 ID”。
转到 Google API 控制台并创建项目。
然后,激活日历。
转到“API 访问”选项,然后使用“创建 OAuth 2.0 客户端”按钮。
为产品提供名称(如果需要,还可以提供徽标)。点击下一步”。
选择“已安装的应用程序”选项并单击“创建客户端 ID”。
现在您的访问权限已配置完毕。现在,您将需要一些代码。要获得它们:
*“验证码”。要获取它,您需要以下信息:
SCOPE: https://www.google.com/calendar/feeds/ https://www.google.com/calendar/feeds/(如果您想访问日历 API。还有其他 API,您可以在 OAuth 2.0 Playground 找到它们)
CLIENT_ID:您可以在 Google API 控制台的 API 访问部分找到它。
REDIRECT_URI:在同一个地方获取它。
现在,将以下代码复制到文件中,将值放入变量中,执行代码 (php -q script_name.php),然后转到打印的 URL。
<?php
$scope = '';
$client_id = '';
$redirect_uri = '';
$params = array(
'response_type' => 'code',
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'scope' => $scope
);
$url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($params);
echo $url."\n";
?>
该网页将要求您允许访问。这样做,您将获得一个代码,这就是您的身份验证代码。
*“刷新代码”。要获得它,您将需要:
您之前使用的数据,加上 API 控制台中“客户端 ID”和“重定向 URI”之间的“客户端密钥”代码。
像之前一样,复制以下代码,并将变量放在适当的位置(代码字段是身份验证代码)。
执行,结果就是“Refresh Token”。
<?php
$url = 'https://accounts.google.com/o/oauth2/token';
$post_data = array(
'code' => '',
'client_id' => '',
'client_secret' => '',
'redirect_uri' => '',
'grant_type' => 'authorization_code',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$token = json_decode($result);
echo $token->refresh_token . "\n";
?>
此时此刻,你已经拥有了你所需要的一切。如果有一天您更改了验证码,请务必小心。您将必须获得新钥匙。
要访问日历服务,这里有示例:
使用前更改变量值。
此示例获取主要日历事件,但您可以更改日历 API 中任何事件的地址 (http://code.google.com/intl/ca/apis/calendar/v3/getting_started.html#background_operations http://code.google.com/intl/ca/apis/calendar/v3/getting_started.html#background_operations)
<?php
$scope = 'https://www.google.com/calendar/feeds/';
$client_id = '';
$client_secret = '';
$redirect_uri = '';
$refresh_token = '';
$token_url = 'https://accounts.google.com/o/oauth2/token';
$post_data = array(
'client_secret' => $client_secret,
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
'client_id' => $client_id
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$token_object = json_decode($result);
$access_token = $token_object->access_token;
// Get the results
$rest_url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events';
$header = "Authorization: OAuth " . $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_URL, $rest_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rest_result = curl_exec($ch);
print_r(json_decode($rest_result));
?>
首先,该脚本要求提供“访问令牌”,该令牌的有效期为一小时。然后,脚本获取 REST 服务(日历范围内的任何服务),并在标头中发送访问令牌。
为了获得最佳的票据速度,最好将访问令牌存储在缓存中,直到它超过 3600 秒。这样,脚本就可以避免这两个调用之一。
Tips:
访问 OAuth 2.0 Playground 以了解 OAuth 流程中发送的所有信息。这对我帮助很大
埃里克·内格尔(Eric Nagel)在他的博客中发表的一篇文章给了我解决方案。所有的功劳都是他的。我无法链接它,因为我没有足够的“声誉”。