php - Google Calendar API v3 - 使用硬编码凭据进行身份验证

标签 php google-api google-api-php-client google-oauth google-calendar-api

我正在编写一个PHP应用程序,该应用程序应该允许用户将某些事件添加到私有(private)的Google日历中。日历是我所有的,我需要一种使用固定凭据与日历API通信的方法(每个人都可以使用网站上的表单添加事件,但日历本身并不公开)。

根据我阅读的内容,这是可以使用V1 API中的客户端胶质素。但是,在V3 API中,可用选项是OAuth2.0或API键。使用API​​ key 似乎不起作用,因为它只能用于不需要授权的请求,而Oauth似乎也不正确,因为用户不应该访问自己的日历,而是我的日历应用程序使用。

我考虑通过编程获取OAuth代币,但这一定会迟早破裂,因为OAuth对话框可以使用CAPTCHAS。

这似乎是一种标准用例 - 一个Web应用程序,可让用户以某些预定义的方式与单个日历进行交互 - 但我找不到有关如何在V3 API中实现它的文档。谁能帮帮我?

最佳答案

我找到了一个解决方案,我认为这是您想要做的事情的“官方”解决方案。

首先,您必须激活 Google API“已安装应用程序的客户端 ID”。

转到 Google API 控制台并创建项目。

然后,激活日历。

转到“API 访问”选项,然后使用“创建 OAuth 2.0 客户端”按钮。

为产品命名(和 Logo ,如果需要)。单击“下一步”。

选择“已安装的应用程序”选项并单击“创建客户端 ID”。

现在您已经配置了访问权限。现在,您将需要一些代码。要获得它们:

*“验证码”。要获取它,您需要以下信息:

范围:https://www.google.com/calendar/feeds/ (如果您想访问日历 API。您可以在 OAuth 2.0 Playground 找到其他 API)

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";
?>

这一刻,你拥有了你所需要的一切。如果有一天您更改了验证码,请小心。您将必须获得新 key 。

要访问日历服务,这里有示例: 在使用之前更改变量值。 此示例获取主要日历事件,但您可以在日历 API ( 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));
    ?>

首先,脚本要求一个“访问 token ”,有效期为一个小时。然后,脚本获取 REST 服务(日历范围内的任何服务),在 header 中发送访问 token 。 为了在 scrip 上获得最佳速度,最好将访问 token 存储在缓存中,直到它超过 3600 秒。这样,脚本将避免这两个调用之一。

提示:

访问 OAuth 2.0 Playground 了解 OAuth 流程中发送的所有信息。对我帮助很大

Eric Nagel 在其博客中的一篇文章为我提供了解决方案。所有的功劳都归于他。我无法链接它,因为我没有足够的“声誉”。

关于php - Google Calendar API v3 - 使用硬编码凭据进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257678/

相关文章:

php - 如何使 session /cookies 在 Yii 的所有子域中工作

php - 未捕获的异常 'Google_AuthException',消息为 'Could not json decode the token'

php - 谷歌云端硬盘 PHP API : Delete shows Insufficient Permission

php - 从 MySQL 数据库获取整数值

C++ 中的 PHP 样式 foreach 循环

google-api - GKE MasterAuth clientCertificate 无权访问集群资源

node.js - 如何使用 SailsJS 调用 Google API

php - BigQuery 使用 PHP 流式传输 'insertAll' 性能

google-drive-api - Google Drive API 获取文件 编辑 URL

PHP:多维数组元素的串联