谷歌推动宁静的api
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌推动宁静的api相关的知识,希望对你有一定的参考价值。
我在NodeJS中使用'passport-google-drive'和'passport.js'库,我的目标是列出所有文件和文件夹,并且能够下载和上传文件到drive.I'm能够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')
);
答案
下面的代码显示了如何使用配置文件信息为用户提供个性化功能。
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 Restful API阅读Node.js的quickstart。
以上是关于谷歌推动宁静的api的主要内容,如果未能解决你的问题,请参考以下文章