How to get a GCP Bearer token programmatically with python

前端 未结 5 1270
余生分开走
余生分开走 2020-12-02 23:37

gcloud auth print-access-token gives me a Bearer token that I can use later on; however, this is a shell command. How would I obtain one programmatically via t

5条回答
  •  被撕碎了的回忆
    2020-12-03 00:25

    This may not be the recommended way but for Rest API in my application this was an easy way to get the token.

    from subprocess import PIPE, Popen
    
    
    def cmdline(command):
        process = Popen(
            args=command,
            stdout=PIPE,
            shell=True
        )
        return process.communicate()[0]
    
    
    token = cmdline("gcloud auth application-default print-access-token")
    print("Token:"+token)
    

提交回复
热议问题