【问题标题】:Getting refresh token and exchanging for access token Google Drive Javascript API?获取刷新令牌并交换访问令牌 Google Drive Javascript API?
【发布时间】:2020-12-11 00:19:22
【问题描述】:

我在前端使用 Google Drive API 和 Google Picker API。我的目标是让用户授权使用他们的驱动器帐户,然后能够在他们需要时调用 Google Picker API,而无需重新授权他们的驱动器。

// Authorizing client initially (I only want them to have to do this once)
await gapi.load("client:auth2", async () => {
  clientInit = window.gapi.auth2.init({
    client_id: driveClientId,
    scope: driveScope,
  });
  await gapi.client.load('drive', 'v2', () => { handleAuthResult(clientInit) });
});

// Setting the access token (expires in 3600s)
async handleAuthResult(authResult) {
    let signInResponse = await authResult.signIn();
    let googleOAuthToken = signInResponse.wc["access_token"];
}

// Creating picker
 new google.picker.PickerBuilder()
    .addView(docsView)
    .setOAuthToken(googleOAuthToken)
    .setCallback(drivePickerCallback)
    .build();

选择器具有 .setOAuthToken(access_token) 方法,该方法将 access_token 作为参数。您在授权驱动器后最初获得的那个会在 1 小时内过期,并且它没有给我一个刷新令牌来获取另一个访问令牌。 signInResponse.wc 不包含刷新令牌。

我很困惑如何获取这个刷新令牌,然后如何使用它来获取访问令牌,文档不太清楚。

【问题讨论】:

    标签: google-oauth access-token drive refresh-token google-api-js-client


    【解决方案1】:

    好的,所以我能够使用 .grantOfflineAccess() 方法解决这个问题。这将返回一个授权代码,该代码可用于通过向https://www.googleapis.com/oauth2/v4/token 发出 POST 请求来交换刷新令牌。

    // Getting refresh token 
    
    let offlineAccess = await signInResponse.grantOfflineAccess();
    
    // Authorization code can be used to exchange for a refresh token
    let refreshAccessCode = offlineAccess.code;  
    let tokenRequest = await axios.request({
        method: 'post',
        url: "https://www.googleapis.com/oauth2/v4/token",
        headers: {"content-type": "application/x-www-form-urlencoded"},
        params: {
          code: refreshAccessCode,
          client_id: this.driveClientId,
          client_secret: this.driveClientSecret,
          redirect_uri: "postmessage",
          grant_type: "authorization_code"
        }
     });
    
    let googleOAuthToken = tokenRequest.data["refresh_token"];
    

    现在您有了刷新令牌,您可以通过发出另一个 POST 请求来将其交换为访问令牌,如下所示:

    // Exchanging refresh token for access code
    
    let tokenRequest = await axios.request({
        method: 'post',
        url: "https://oauth2.googleapis.com/token",
        headers: {"content-type": "application/x-www-form-urlencoded"},
        params: {
          client_id: driveClientId,
          client_secret: driveClientSecret,
          refresh_token: googleOAuthToken,
          grant_type: "refresh_token"
        } 
     });
      
    let accessToken = tokenRequest.data["access_token"];
    

    【讨论】:

    • 您在哪里运行此代码(在浏览器或后端服务中)?
    • @JohnHanley 你解决了吗?尝试将 Google Picker 与离线 access_token(新生成的 refresh_token)一起使用,但没有运气。 Google Picker 似乎无论如何都想要一个用户会话?
    • @aalimovs - 我不确定你的意思。答案不是我的。我查看了答案,它似乎是正确的。我建议您使用您的详细信息、错误等创建一个新问题。
    猜你喜欢
    • 2019-03-16
    • 2012-09-16
    • 2013-06-14
    • 2014-07-15
    • 2015-01-08
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多