如何在服务器端扩展在客户端获得的Google OAuth令牌

时间:2019-02-21 17:43:39

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

我有一个Web应用程序,该应用程序具有基于EmberJS构建的前端和Java后端。

我正在使用户使用Google OAuth登录并请求范围:https://www.googleapis.com/auth/calendar

{
  "El": "google_user_id",
  "Zi": {
    "token_type": "Bearer",
    "access_token": "access_token",
    "scope": "openid email profile https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/calendar",
    "login_hint": "login_hint",
    "expires_in": 3600,
    "id_token": "..AF16AF6oc7Fl2uv5V9r",
    "session_state": {
      "extraQueryParams": {
        "authuser": "0"
      }
    },
    "first_issued_at": 1550770587899,
    "expires_at": 1550774187899,
    "idpId": "google"
  },
  "w3": {
    "Eea": "google_user_id",
    "ig": "Shivang Tripathi",
    "ofa": "Shivang",
    "wea": "Tripathi",
    "Paa": "https://mnsbfsdbf/photo.jpg",
    "U3": "shivang.tripathi@org.com"
  }
}

现在,我将此响应发送到服务器。 服务器可以使用提供的“ access_token”来执行各种任务,例如通过使用REST进行API调用来获取日历列表等。 例如。以“ access_token”作为授权标头拨打https://www.googleapis.com/calendar/v3/users/me/calendarList

但是我遇到了一个问题。 令牌寿命短,并且会在60分钟后失效。

我可以以某种方式将此令牌扩展为永无止境的令牌或寿命长的令牌吗? Facebook允许这样做:https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing 有人可以告诉我Google是否从服务器端允许此操作吗?

2 个答案:

答案 0 :(得分:0)

结果是没有办法。

我不得不走另一条路线,那就是: https://developers.google.com/identity/protocols/OAuth2WebServer

  1. 生成身份验证URL并将用户重定向到该URL: https://accounts.google.com/o/oauth2/v2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&access_type=offline&include_granted_scopes=true&state=state_parameter_passthrough_value&redirect_uri=http://localhost:5000/callback&response_type=code&client_id=

  2. 处理回调,它具有授权码

  3. 使用该代码获取刷新令牌和访问令牌:

  4. POST / oauth2 / v4 /令牌HTTP / 1.1

    主持人:www.googleapis.com 内容类型:application / x-www-form-urlencoded

    code = 4 / P7q7W91a-oMsCeLvIaQm6bTrgtp7& client_id =您的client_id& client_secret =您的client_secret& redirect_uri = https://oauth2.example.com/code& grant_type = authorization_code

答案 1 :(得分:0)

说“没有其他办法”是不正确的。正确的方法是再次发出身份验证请求,但添加“ prompt = none”。 btw“ login_prompt”应该是电子邮件地址或Google用户ID。