【发布时间】: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