Send a report CSV to Google Cloud Storage using API

To send a CSV data to Google Cloud Storage, we require

A JWT token generated using the client credentials JWT token library script is added below

https://jobinandjismi.in/jwt-token-library-script/

Below code defines generating JWT token using library script

let tokenResponse = getOauthToken(JWT_TOKEN_INPUT)
function getOauthToken(JWT_TOKEN_INPUT) {
            let jwt_header = JWT_TOKEN_INPUT.JWT_HEADER;
            let jwt_payload = JWT_TOKEN_INPUT.JWT_PAYLOAD;
            let private_key = JWT_TOKEN_INPUT.PRIVATE_KEY.keyValue;
            let JWT_TOKEN = JWTToken._genJWS(jwt_header, jwt_payload, private_key); 
//JWTToken refers to library
            let oauthHeaderObj = {
                'Content-Type': 'application/json',
            };
            let oAuthUrl = `https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${JWT_TOKEN}`;
            let oAuthTokenResponse = https.post({
                url: oAuthUrl,
                headers: oauthHeaderObj
            });
            let responseBody = JSON.parse(oAuthTokenResponse.body);
            log.debug("responseBody", responseBody);
            return responseBody;
        }
// Using token, send data to the GCS bucket
let accessToken = tokenResponse.access_token;
                    log.debug("Token", accessToken)
                    let response = https.post({
                        url: 'https://storage.googleapis.com/upload/storage/v1/b/xseed-jobin-jismi-test-bucket/o?uploadType=media&name=GeneralLedger' + currentTime + year + '.csv',
                        headers: {
                            "Content-Type": "text/csv",
                            "Authorization": "Bearer " + accessToken
                        },
                        body: sendData
                    });

Leave a comment

Your email address will not be published. Required fields are marked *