Google 日历 API 在 OAuth 期间给出 403 错误,即使它似乎授予访问权限
Google Calendar API gives 403 error during OAuth even though it seems to grant access
我的应用程序将发布、编辑和删除用户 Google 日历上的活动。我正在配置 OAuth 来实现这一点。这个想法是,在注册后,用户将能够进入他们的设置并同意将我的应用程序连接到他们的 Google 日历。然后,我将能够将 oauth 令牌和刷新令牌存储在数据库中,并在我 create/edit/delete 用户日历上的事件时使用它们。
无论如何,问题是我选择我的帐户:
然后我通过单击 "Allow" 表示同意:
这是奇怪的地方:在幕后,Google 日历 API 报告 403 禁止错误。
%Ueberauth.Failure{errors: [%Ueberauth.Failure.Error{message: 403,
message_key: "OAuth2"}], provider: :google,
strategy: Ueberauth.Strategy.Google}
我的 ueberauth
配置:
config :ueberauth, Ueberauth,
providers: [
google: {Ueberauth.Strategy.Google, [default_scope: "https://www.googleapis.com/auth/calendar", approval_prompt: "force", access_type: "offline"]}
]
我提出的要求:
def callback(%{assigns: %{ueberauth_failure: fail}} = conn, _params) do
IO.inspect fail
conn
|> put_flash(:error, "Failed to authenticate.")
|> redirect(to: "/")
end
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
IO.inspect auth
conn
|> put_flash(:success, "Connected to Google.")
|> redirect(to: "/")
end
第一个 callback
函数匹配(因为它失败了)。
但是,当我转到我的Google帐户时,我可以看到该应用已被授予权限:
我提供了正确的 client_id 和 client_secret。此外,我在 Google API 控制台中创建了一个服务帐户,并与该帐户共享了我的日历:
我还需要做什么?
编辑: 更多信息 - 我可以通过我的代码(即样板 Ueberauth_Google)授予对所有其他 Google 模块的访问权限。例如,如果我以电子邮件作为范围发出请求,它会工作并且我从 Google 获得 auth_token。只有 Google Calendar 给出了 403,这让我相信有一些特定的东西导致了它。
编辑 2: 我查看了 error handling section of the Google Calendar API,那里列出的 403 错误中的 none 适用于我:
- 403:超出每日限额
- 403:超出用户速率限制
- 403:超出速率限制
- 403:超出日历使用限制
编辑 3: 我创建了一个全新的 Google 帐户并与我的 Google 服务帐户共享了它的日历。那个给出了同样的错误。
终于想通了。
https://www.googleapis.com/auth/calendar
范围本身是不够的。还需要将 https://www.googleapis.com/auth/userinfo.profile
添加到范围。
我的应用程序将发布、编辑和删除用户 Google 日历上的活动。我正在配置 OAuth 来实现这一点。这个想法是,在注册后,用户将能够进入他们的设置并同意将我的应用程序连接到他们的 Google 日历。然后,我将能够将 oauth 令牌和刷新令牌存储在数据库中,并在我 create/edit/delete 用户日历上的事件时使用它们。
无论如何,问题是我选择我的帐户:
然后我通过单击 "Allow" 表示同意:
这是奇怪的地方:在幕后,Google 日历 API 报告 403 禁止错误。
%Ueberauth.Failure{errors: [%Ueberauth.Failure.Error{message: 403,
message_key: "OAuth2"}], provider: :google,
strategy: Ueberauth.Strategy.Google}
我的 ueberauth
配置:
config :ueberauth, Ueberauth,
providers: [
google: {Ueberauth.Strategy.Google, [default_scope: "https://www.googleapis.com/auth/calendar", approval_prompt: "force", access_type: "offline"]}
]
我提出的要求:
def callback(%{assigns: %{ueberauth_failure: fail}} = conn, _params) do
IO.inspect fail
conn
|> put_flash(:error, "Failed to authenticate.")
|> redirect(to: "/")
end
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
IO.inspect auth
conn
|> put_flash(:success, "Connected to Google.")
|> redirect(to: "/")
end
第一个 callback
函数匹配(因为它失败了)。
但是,当我转到我的Google帐户时,我可以看到该应用已被授予权限:
我提供了正确的 client_id 和 client_secret。此外,我在 Google API 控制台中创建了一个服务帐户,并与该帐户共享了我的日历:
我还需要做什么?
编辑: 更多信息 - 我可以通过我的代码(即样板 Ueberauth_Google)授予对所有其他 Google 模块的访问权限。例如,如果我以电子邮件作为范围发出请求,它会工作并且我从 Google 获得 auth_token。只有 Google Calendar 给出了 403,这让我相信有一些特定的东西导致了它。
编辑 2: 我查看了 error handling section of the Google Calendar API,那里列出的 403 错误中的 none 适用于我:
- 403:超出每日限额
- 403:超出用户速率限制
- 403:超出速率限制
- 403:超出日历使用限制
编辑 3: 我创建了一个全新的 Google 帐户并与我的 Google 服务帐户共享了它的日历。那个给出了同样的错误。
终于想通了。
https://www.googleapis.com/auth/calendar
范围本身是不够的。还需要将 https://www.googleapis.com/auth/userinfo.profile
添加到范围。