【问题标题】:Unable to refresh access_token - google calendar api无法刷新 access_token - 谷歌日历 api
【发布时间】:2019-10-28 03:59:23
【问题描述】:

问候我的开发者伙伴 我正在开发一个项目,客户需要将 Google 日历与该项目连接起来。 我正在使用 GAPI 将 Google 日历与我的应用程序连接起来。 这是我正在做的事情:

第 1 步:

let config = {
    apiKey: [API_KEY],
    clientId: [CLIENT_ID],
    access_type: 'offline',
    discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest'],
    scope: 'https://www.googleapis.com/auth/calendar.events'
}
gapi.load('client:auth2', () => {
    gapi.client.init(config).then(() => {
        gapi.auth2.getAuthInstance().isSignedIn.listen(true);
    });
});

第 2 步:

Promise.resolve(gapi.auth2.getAuthInstance().signIn()).then(response => {
    store access_token, id_token, login_hint
}).then(() => {
    run_different_axios_requests_to_google_calendar();
});

或者如果我已经拥有访问令牌:

run_different_axios_requests_to_google_calendar();

到目前为止它运行完美!

访问令牌过期时会出现问题,并阅读文档,我发现如果您运行 gapi.auth2.getAuthInstance(). grantOfflineAccess() 而不是 gapi.auth2.getAuthInstance().signIn() 您会得到 refresh_token 以及 access_tokenid_tokenlogin_hint,但是我没有得到回应,事实上我的承诺表明This app isn't verified,并且我需要在 access_token 过期时刷新令牌,以便我可以获得新的令牌,而不必要求用户再次登录(通过承诺)。我不确定我在这里做错了什么!任何帮助表示赞赏!

【问题讨论】:

    标签: javascript google-calendar-api google-api-js-client


    【解决方案1】:

    这是来自 Google 的 complete documentation,可让您使用新的 JS 库来解锁 Google Apps 的强大功能。它详细解释了从身份验证到使用日历 API 等 GAPI 库的逐步过程。

    【讨论】:

    • 你好,杰斯!我已经尝试过您提到的文档,它正在工作,因此无需在我端保存 access_token(以供将来调用),但它解决了这个问题,但随后又显示了另一个问题,我希望能够获取日历事件来自登录用户,这意味着,它应该能够根据哪个用户登录进行调用,这样做是它请求一次许可,然后使用该许可响应进行进一步调用,如果新用户登录在,呼叫仍然是以前的用户而不是新用户!我希望我已经很好地解释了我的观点,
    猜你喜欢
    • 2021-07-28
    • 2021-05-06
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2015-06-21
    • 2023-03-26
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多