【问题标题】:Meteor accounts-google Token ExpiresMeteor 账户-google Token Expires
【发布时间】:2015-12-22 06:12:10
【问题描述】:

我有 Accounts-UI 配置设置来为谷歌存储离线令牌:

if (Meteor.isClient) {
 Accounts.ui.config({
  requestOfflineToken: { google: true },
  forceApprovalPrompt: { google: true },
  requestPermissions: { google: ["https://mail.google.com/"] }
 });
}

但是,令牌似乎已过期。我假设我需要以某种方式使用 refreshToken。我不确定如何使用流星。任何帮助都会很可爱。谢谢!

【问题讨论】:

    标签: javascript meteor meteor-accounts


    【解决方案1】:

    我建议使用 Google API Node JS 客户端来刷新您的访问令牌。

    https://github.com/google/google-api-nodejs-client/

    它作为服务器端 NPM 包提供,因此您可能希望使用 this package 以便能够在您的 Meteor 应用程序中使用 npmRequire。

    使用此packages.json 配置加载最新的googleapis 包:

    {
      "googleapis": "2.1.5"
    }
    

    然后在您的 Meteor 服务器代码中,您将能够像这样刷新访问令牌:

    ES2015

    const GoogleApis = Meteor.npmRequire('googleapis');
    
    function getAccessToken(user) {
      const googleService = user.services.google;
      // is token still valid for the next minute ?
      if (googleService.expiresAt < Date.now() + 60 * 1000) {
        // then just return the currently stored token
        return {
          access_token: googleService.accessToken,
          token_type: 'Bearer',
          id_token: googleService.idToken,
          expiry_date: googleService.expiresAt,
          refresh_token: googleService.refreshToken,
        };
      }
      // fetch google service configuration
      const googleServiceConfig = Accounts.loginServiceConfiguration.findOne({
        service: 'google',
      });
      // declare an Oauth2 client
      const oauth2Client = new GoogleApis.auth.OAuth2(googleServiceConfig.clientId, googleServiceConfig.secret);
      // set the Oauth2 client credentials from the user refresh token
      oauth2Client.setCredentials({
        refresh_token: user.services.google.refreshToken,
      });
      // declare a synchronous version of the oauth2Client method refreshing access tokens
      const refreshAccessTokenSync = Meteor.wrapAsync(oauth2Client.refreshAccessToken, oauth2Client);
      // refresh tokens
      const tokens = refreshAccessTokenSync();
      // update the user document with the fresh token
      Meteor.users.update(user._id, {
        $set: {
          'services.google.accessToken': tokens.access_token,
          'services.google.idToken': tokens.id_token,
          'services.google.expiresAt': tokens.expiry_date,
          'services.google.refreshToken': tokens.refresh_token,
        },
      });
      //
      return tokens;
    }
    

    这是一个完整示例,说明如何在使用 Google 服务之前刷新您的访问令牌。

    function listMeteorChannel() {
      // fetch a user you want to act on behalf who authorized offline access
      const user = Meteor.users.findOne({
        'services.google.refreshToken': {
          $exists: true,
        },
      });
      if (!user) {
        return;
      }
      const googleServiceConfig = Accounts.loginServiceConfiguration.findOne({
        service: 'google',
      });
      // declare oauth2 client and set credentials
      const oauth2Client = new GoogleApis.auth.OAuth2(googleServiceConfig.clientId, googleServiceConfig.secret);
      // get user access token
      const tokens = getAccessToken(user);
      oauth2Client.setCredentials(tokens);
      // obtain the youtube service at version 3 and perform authentication at service level
      const youtube = GoogleApis.youtube({
        version: 'v3',
        auth: oauth2Client,
      });
      // declare a synchronous version of youtube.channels.list
      const youtubeChannelsListSync = Meteor.wrapAsync(youtube.channels.list, youtube.channels);
      // fetch an info snippet from the Meteor official YouTube channel
      const result = youtubeChannelsListSync({
        part: 'snippet',
        // Meteor channel ID
        id: 'UC3fBiJrFFMhKlsWM46AsAYw',
      });
      result.items.forEach((item) => {
        // display the channel title, which should be 'Meteor'
        console.log(item.snippet.title);
      });
    }
    
    Meteor.startup(listMeteorChannel);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-08
      • 2020-12-21
      • 1970-01-01
      • 2023-04-05
      • 2014-11-12
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      相关资源
      最近更新 更多