0
votes

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

1 Answers

2
votes

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