Google Drive for Restful API

时间:2018-09-09 08:53:52

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

我正在使用NodeJS中的'passport-google-drive'和'passport.js'库,我的目标是列出所有文件和文件夹,并能够将文件下载和上传到drive。能够0auth用户与谷歌驱动器,然后我得到acessToken,用户的个人资料。 之后,下一步该怎么做?如何使用acessToken和用户个人资料列出所有文件和文件夹,以及如何从驱动器下载上传文件。

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')
   );

1 个答案:

答案 0 :(得分: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')
   );

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

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