【问题标题】:Google drive for restful api用于restful api的谷歌驱动器
【发布时间】:2018-09-09 08:53:52
【问题描述】:

我在 NodeJS 中使用“passport-google-drive”和“passport.js”库,我的目标是列出所有文件和文件夹,并能够下载和上传文件到驱动器。我是能够使用谷歌驱动器对用户进行 0auth 验证,然后我得到 accessToken,用户的个人资料。 之后下一步该做什么?如何使用 accessToken 和用户配置文件列出所有文件和文件夹,并能够从驱动器下载上传文件。

const passport = require('passport');
const GoogleDriveStrategy = require('passport-google-drive').Strategy;
const mongoose = require('mongoose');
const Keys = require('../config/keys.js');
const User = mongoose.model('drive-users');
const prettyjson = require('prettyjson');


passport.use(
    new GoogleDriveStrategy(
     {
       clientID: Keys.DRIVE_CLIENT_ID,
       clientSecret: Keys.DRIVE_CLIENT_SECRET,
       callbackURL: '/auth/google-drive/callback',
       scope : 'https://www.googleapis.com/auth/drive'
      },
      (accessToken, refreshToken, profile, done) => {
        console.log(prettyjson.render(profile));
        //what next????
          }
       )
   );
   //==================
   //routes
   //==================
   
   
   app.get(
    '/auth/google-drive',
     passport.authenticate('google-drive')
   );

  app.get(
    '/auth/google-drive/callback',
     passport.authenticate('google-drive')
   );

【问题讨论】:

    标签: javascript node.js api google-drive-api passport.js


    【解决方案1】:

    以下代码展示了如何使用个人资料信息为用户提供个性化功能。

    const passport = require('passport');
    const GoogleDriveStrategy = require('passport-google-drive').Strategy;
    const mongoose = require('mongoose');
    const Keys = require('../config/keys.js');
    const User = mongoose.model('drive-users');
    const prettyjson = require('prettyjson');
    
    function extractProfile (profile) {
        let imageUrl = '';
        if (profile.photos && profile.photos.length) {
          imageUrl = profile.photos[0].value;
        }
        return {
          id: profile.id,
          displayName: profile.displayName,
          image: imageUrl
        };
      }
    
    passport.use(
        new GoogleDriveStrategy(
         {
           clientID: Keys.DRIVE_CLIENT_ID,
           clientSecret: Keys.DRIVE_CLIENT_SECRET,
           callbackURL: '/auth/google-drive/callback',
           scope : 'https://www.googleapis.com/auth/drive'
          },
          (accessToken, refreshToken, profile, done) => {
            console.log(prettyjson.render(profile));
              // Extract the minimal profile information we need from the profile object
              // provided by Google
              done(null, extractProfile(profile));
              }
           )
       );
    
        // Typically, this will be as simple as storing the user ID when serializing, and finding
        //  the user by ID when deserializing.
        passport.serializeUser( function (user, done) {
            done(null, user);
        });
    
        passport.deserializeUser( function (obj, done) {
            done(null, obj);
        });
    
       //==================
       // add your routes here
       //==================
    
    
       app.get(
        '/auth/google-drive',
         passport.authenticate('google-drive')
       );
    
      app.get(
        '/auth/google-drive/callback',
         passport.authenticate('google-drive')
       );
    

    还有一个使用 Passport.js 的 sample。这将帮助您了解使用 Passport.js 库的用户身份验证。

    您还可以使用 Google Drive for Resful API 阅读 Node.js 的 quickstart

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2018-04-13
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多