Гугл диск для успокоительного API - PullRequest
0 голосов
/ 09 сентября 2018

Я использую библиотеки 'passport-google-drive' и 'passport.js' в NodeJS, и моя цель - перечислить все файлы и папки, а также иметь возможность загружать и выгружать файлы на диск. возможность 0auth пользователя с Google Drive, и после этого я получаю 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 голосов
/ 11 сентября 2018

Код ниже показывает, как использовать информацию профиля для предоставления пользователям персонализированных функций.

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. Это поможет вам понять аутентификацию пользователей с помощью библиотеки Passport.js.

Вы также можете прочитать быстрый запуск Node.js, используя Google Drive для Resful API.

...