【问题标题】:How do I connect to the Google Calendar API without the oAuth authentication?如何在没有 oAuth 身份验证的情况下连接到 Google Calendar API?
【发布时间】:2012-02-18 05:00:08
【问题描述】:

我一直在研究 Google Calendar API 和有关身份验证的文档 (http://code.google.com/apis/calendar/v3/using.html#auth)。看来这里提到的用例是编写一个访问用户日历的应用程序。但是,我正在编写一个网页,该网页将访问网站所有者的日历并仅显示该日历的信息。所以,我不希望用户输入他们的谷歌账户信息,这是 oAuth 想要做的。

基本上,我正在寻找一种方法来访问单个私人 Google 日历并通过将凭据直接传递给服务对其进行身份验证。

这里有类似的问题:How to use OAuth with Google Calendar to access only ONE calendar? 这似乎表明张贴者最初是直接传递凭据。这个功能还能用吗?您如何处理我描述的用例?

【问题讨论】:

    标签: oauth google-api google-calendar-api


    【解决方案1】:

    如果我没记错的话,他们现在已经为此推出了服务帐户:https://developers.google.com/accounts/docs/OAuth2ServiceAccount

    编辑:

    这是他们的 Prediction API 的修改

        session_start();
        require_once "google-api-php-client/src/Google_Client.php";
        require_once "google-api-php-client/src/contrib/Google_CalendarService.php";
    
        const CLIENT_ID = '...';
        const SERVICE_ACCOUNT_NAME = '...';
    
        // Make sure you keep your key.p12 file in a secure location, and isn't
        // readable by others.
        const KEY_FILE = '...';
    
        $client = new Google_Client();
        $client->setApplicationName("...");
    
    
        if (isset($_SESSION['token'])) {
         $client->setAccessToken($_SESSION['token']);
        }
    
        // Load the key in PKCS 12 format (you need to download this from the
        // Google API Console when the service account was created.
        $key = file_get_contents(KEY_FILE);
        $client->setAssertionCredentials(new Google_AssertionCredentials(
            SERVICE_ACCOUNT_NAME,
            array('https://www.googleapis.com/auth/calendar', "https://www.googleapis.com/auth/calendar.readonly"),
            $key)
        );
    
        $client->setClientId(CLIENT_ID);
        $service = new Google_CalendarService($client);
    
        //Save token in session
        if ($client->getAccessToken()) {
          $_SESSION['token'] = $client->getAccessToken();
        }
    
        //And now you can use the code in their PHP examples, like: $service->events->listEvents(...)
    

    【讨论】:

    • 是的。我从他们的 Prediction API 修改了服务代码,它适用于日历
    • @gX。请问你能分享你的代码吗?我正在尝试做同样的事情但没有成功:(
    【解决方案2】:

    我也有同样的问题。我发现这个由 3 部分组成的教程正是你想要的。 Web page for bookings
    您创建的网页直接写入您自己的日历(而不是用户),这就是用户不必授予您访问其日历的权限的原因。
    此解决方案适用于我,但您必须直接使用 REST 进行管理。

    我正在寻找一种使用 google api php 客户端的方法,因为它看起来有点简单。但是我还没有找到一种方法来正确编辑 google-api-php-client/src/config.php 文件,以便以一种您验证自己而不是实际用户的方式工作,因为您需要访问您的日历。如果有人有想法或脚本可以做到这一点,我将非常感激。

    【讨论】:

    • 您指向的 3 部分文章似乎确实是答案。我将一有机会就尝试一下,如果可行,请将其标记为答案。我本来打算使用 REST API 并只处理 JSON 数据,所以这应该让我继续前进。谢谢!
    • 这成功了!关键是将应用程序视为“已安装的应用程序”。在临时装配某些东西以获取刷新令牌后,您可以随后使用刷新令牌获取新的访问令牌,而无需提示用户。
    【解决方案3】:

    也看看这里:

    https://github.com/googleapis/google-api-php-client#authentication-with-service-accounts

    创建服务帐户后,您可以在没有用户提示的情况下进行身份验证,以将项目添加到您的服务拥有的日历中。

    【讨论】:

      猜你喜欢
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多