前端验证请求Google Cloud Storage [英] Frontend Authenticated request Google Cloud Storage

查看:116
本文介绍了前端验证请求Google Cloud Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Cloud Storage存储桶上传一些用户文件.我不希望它们公开显示,所以我创建了一个代表我的前端应用程序的服务帐户.

I am using a Google Cloud Storage bucket to upload some of my users files. I do not want them to appear as public, so I created a service account representing my frontend app.

我想知道如何在不使用前端应用程序中的@google-cloud/storage npm软件包的情况下向Google Cloud Storage发出身份验证请求.

I want to know how to make Authenticated Request to Google Cloud Storage, without using the @google-cloud/storage npm package from my frontend app.

我知道我需要在请求标头中包含Auhtorization: Bearer <token>,但是,如何获得此令牌?

I know I need to include Auhtorization: Bearer <token> in my request headers but, how do I get this token ?

我在前端应用程序上使用React.

I'm using React on my frontend app.

推荐答案

Google有许多可以使用的库.这是一个示例:

Google has a number of libraries that you can use. Here is one example:

var { google } = require('googleapis')
const request = require('request')

// The service account JSON key file to use to create the Access Token
let privatekey = require('/path/service-account.json')

let scopes = 'https://www.googleapis.com/auth/devstorage.read_only'

let jwtClient = new google.auth.JWT(
    privatekey.client_email,
    null,
    privatekey.private_key,
    scopes
)

jwtClient.authorize(function(err, _token) {
    if (err) {
        console.log(err)
        return err
    } else {
        console.log('token obj:', _token)
        console.log('access token:', _token.access_token)

        headers: {
            "Authorization": "Bearer " + _token.access_token
        }

        // Make requests to Cloud Storage here
    }
})

这篇关于前端验证请求Google Cloud Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆