Search code examples
restpostmangoogle-cloud-storagepostman-pre-request-script

How to auto generate new Bearer Token in Postman for GCP Storage


I am trying to upload file from local to GCP bucket through cloud storage Rest API (https://storage.googleapis.com/upload/storage/v1/b) using Postman.

I am using Bearer Token for authorization and running $(gcloud auth print-access-token) command on GCP Shell to generate that token every time.

I need to know, how to auto generate that token from Postman while sending request ?

Is there any way to execute $(gcloud auth print-access-token) every time as a Pre-request Script within Postman ?

Thanks


Solution

  • I'm not very good with postman, but I think you can run pre-request to get token and reuse it in the subsequent request.

    If so, you can get inspiration from the gcloud auth print-access-token command by adding the --log-http param to visualize the request performed by the CLI and to reproduce them in Postman.


    EDIT 1

    If you perform the request, you can see that a post is performed to this URL https://oauth2.googleapis.com/token

    To reproduce the call, you can try with a curl

    curl -X POST -d "grant_type=refresh_token&client_id=32555940559.apps.googleusercontent.com&client_secret=ZmssLNjJy2998hD4CTg2ejr2&refresh_token=<REFRESH_TOKEN>&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth" https://oauth2.googleapis.com/token
    

    In this call, you need your REFRESH_TOKEN, that you can get here

    cat ~/.config/gcloud/legacy_credentials/<YOUR EMAIL>/adc.json