我使用护照通过 Google API 进行身份验证,我通过 URL 向客户端(React 应用程序)发送令牌,并将其保存在 localStorage 中。
我想使用该令牌:对于每个 API 调用(获取、发布、放置),我想将该令牌发送到服务器,但我不知道如何在服务器端验证该令牌。
护照策略:
app.use(passport.initialize()); // Used to initialize passport
app.use(passport.session()); // Used to persist login sessions
passport.use(new GoogleStrategy({
clientID: 'IDxxxxx',
clientSecret: 'SecreXXX',
callbackURL: 'http://localhost:3000/callback'
},
(accessToken, refreshToken, profile, done) => {
// Directory API here
var userData = {
name: profile.displayName,
token: accessToken
};
done(null, userData);
Run Code Online (Sandbox Code Playgroud)
验证 :
app.get('/auth/google', passport.authenticate('google', {
scope: ['profile'] // Used to specify the required data
}));
// The middleware receives the data from Google and …Run Code Online (Sandbox Code Playgroud) 我正在与 NodeJs/express 合作创建一个 POST api 端点以将文件上传到谷歌云存储并返回这样的公共 URL:https ://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME ]
上传完成后,我得到了 URL,当我打开它时,直接下载文件(图像、pdf 等...)
有没有办法在浏览器中查看和打开它?
这是我的上传功能:
const uploadImage = (file) => new Promise((resolve, reject) => {
const { originalname, buffer } = file
const blob = bucket.file(originalname.replace(/ /g, "_"))
const blobStream = blob.createWriteStream({
resumable: false
})
blobStream.on('finish', () => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
)
resolve(publicUrl)
})
.on('error', () => {
reject(`Unable to upload image, something went wrong`)
})
.end(buffer)
})
Run Code Online (Sandbox Code Playgroud)
谢谢
node.js express google-cloud-storage google-cloud-platform multer