服务器的 Google API 身份验证

我一直在尝试让 Google 的日历 API 在 PHP 网络中运行申请,但我很难通过身份验证.

I've been trying to get Google's Calendar API working in a PHP web application, but I'm having a hard time getting authenticated.

我想要做的是允许用户与服务器已知的单个帐户的日历进行交互.

What I want to do is to allow users to interact with calendars of a single account known by the server.

OAuth 2.0 文档 中涵盖的每种类型的场景都谈论用户同意"这涉及登录表单和个人用户登录,但我希望服务器本身直接进行身份验证并为自己获取访问令牌.

Each type of scenario covered in the OAuth 2.0 docs talks about "user consent" which involves a login form and the individual user logging in, but I want the server itself to authenticate directly and obtain an access token for itself.

是否有 OAuth 的某些部分或我可以使用的替代机制来执行此操作?

Is there some part of OAuth or some alternative mechanism I can use to do this?

推荐答案

为此,您必须完成用户同意的步骤,然后将其提供给您的访问令牌复制到 PHP 代码中.

In order to do this, you must go through the steps for user consent and then copy the access tokens it gives you into the PHP code.

OAuth 通常的流程是这样的:

The usual procedure for OAuth is like this:

  1. 将用户发送到身份验证页面.
  2. 用户返回 $_GET['code']
  3. 将 $_GET['code'] 发送到 OAuth 服务器以获取令牌
  4. 在数据库中为用户存储令牌(或会话,如果它非常短暂)

但是当使用这样的单个日历进行操作时,您需要修改步骤 4.相反,您将令牌转储到屏幕并将其作为变量复制到您的 PHP 文件中,而不是将其放入数据库中.然后,当您将访问令牌传递给服务器时,您只需传递已知的静态令牌,而不是来自数据库/会话的动态令牌.

But when doing it with a single calendar like this, you modify step 4. Instead, you dump the token to screen and copy it into your PHP file as variables, instead of putting it in the database. Then when you go to pass the access token to the server, you just pass the known, static token rather than a dynamic token from the database / session.

相关文章