【问题标题】:Google Calendar client javascript missing refresh token谷歌日历客户端javascript缺少刷新令牌
【发布时间】:2020-08-19 17:57:18
【问题描述】:

我正在尝试将 Google 日历应用到我们的网络项目中。在搜索并尝试了我包中的几乎所有技巧之后,在用户同意访问日历后,我无法获得刷新令牌。我想在我的前端(访问令牌和刷新令牌)获取用户的日历访问权限,并为其提供后端(python)以便稍后刷新访问令牌。 在尝试了几种可用的解决方案之后,我正在使用这个 - 在 html -

<script src="https://apis.google.com/js/client.js"></script>

在 javascript 中点击按钮 -

gapi.auth.authorize({
         client_id: xxx.apps.googleusercontent.com,
         client_secret: 'XYZ',
         scope: "https://www.googleapis.com/auth/calendar", 
         immediate: false,
         response_type: 'code token',
         access_type:'offline', 
         prompt:'consent'
}, handleAuthResult);

由此我能够得到以下响应 -

    {
  "token_type": "Bearer",
  "access_token": "XXX",
  "scope": "email profile openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar",
  "login_hint": "XXX",
  "expires_in": "3559",
  "id_token": "XXX",
  "session_state": null,
  "expires_at": "1597689877",
  "code": "4/3XXXXX",
  "status": {
    "signed_in": true,
    "method": "PROMPT",
    "google_logged_in": true
  },
  "g-oauth-window": null,
  "client_id": "XXX.apps.googleusercontent.com",
  "cookie_policy": "single_host_origin",
  "response_type": "code token",
  "authuser": "2",
  "issued_at": "1597686278"
}

但仍然没有刷新令牌的迹象,我尝试通过此 api 交换访问令牌和 ID 令牌的代码 -

let request_params = JSON.stringify({
        code: '4/3XXXXX',
        client_id: 'XXX.apps.googleusercontent.com',
        client_secret: 'XXX',
        redirect_uri: 'http://localhost:5000',
        grant_type: 'authorization_code'
    });
    $.ajax({
        url: 'https://oauth2.googleapis.com/token',
        type: 'POST',
        data: request_params,
        headers: {
            'Content-type': 'application/x-www-form-urlencoded'
        },
        contentType: "application/x-www-form-urlencoded",
        success: (res) => {
            console.log(res, 'tokens')
        },
        error: (error) => {
            console.log('An error occurred', error)
        }
    });

但这也给出了一个无效的 grant_type 错误。 任何帮助都非常受欢迎。 另外请让我知道是否有任何替代方法可以让我使用我的服务器直接重定向以获取身份验证而不是 javascript。

【问题讨论】:

  • 客户端授权不返回刷新令牌,这将是一场安全噩梦。使用服务器端语言。
  • 是的,这就是我之前的想法,并尝试通过将 python sdk 用于谷歌日历来做到这一点,但是通过运行本地服务器获得凭据,我避免这样做,因为它只会有帮助我在本地主机上。关于如何在不运行本地服务器的情况下执行此操作的任何建议?
  • 如果您不想运行服务器,则在访问令牌过期时请求用户访问将是您唯一的选择。如果您没有服务器来托管它,您将如何托管它?

标签: javascript ajax oauth-2.0 google-calendar-api refresh-token


【解决方案1】:

我能够在 python 中的 OAuth2WebServerFlow 的帮助下解决这个问题。 这是我遵循的过程 -

  1. 创建一个流实例,例如 -

flow = OAuth2WebServerFlow(client_id='XXX', client_secret='XXX', scope='https://www.googleapis.com/auth/calendar', redirect_uri='your_redirect_url', access_type='offline', prompt='consent')

  1. 重定向到-auth_uri = flow.step1_get_authorize_url()获得的授权url

  2. 身份验证后,用户将被重定向到您提供的redirect_url以及作为参数的code

  3. 将此代码传递到后端以使用 - credentials = flow.step2_exchange(code) 获取访问和刷新令牌。这里的流将是使用第 1 步生成的同一流实例,并且凭据将具有令牌。

注意 - 如果您已经生成了有效的刷新令牌并尝试再次授予访问权限,Google 不提供令牌,并且 step2_exchange 会生成无效的授权错误。

【讨论】:

    猜你喜欢
    • 2021-05-06
    • 2015-03-04
    • 1970-01-01
    • 2013-09-13
    • 2015-08-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多