Frontend Authenticated request Google Cloud Storage

微笑、不失礼 提交于 2020-04-18 03:58:43

问题


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.

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.

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

I'm using React on my frontend app.


回答1:


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
    }
})


来源:https://stackoverflow.com/questions/61256106/frontend-authenticated-request-google-cloud-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!