Search code examples
javagoogle-cloud-platformtokengcloud

Generate GCP auth identity-token in JAVA


I am looking for ways to generate GCP identity token in Java. I am able to generate it via the gcloud cli like this ./bin/gcloud auth print-identity-token. But couldn't find a way to do something similar with the Java Client.

Please share some insights on how this needs to be created. Thanks in advance.


Solution

  • So, in my case the target audience url is something like this XXX.apps.googleusercontent.com I obtained the URL by decoding the token that I generated using the command gcloud auth print-identity-token.

    I used this code to extract it,

    import base64
    
    token = '##update your token here"
    
    b64_profile = token.split('.')[1]
    
    profile = base64.b64decode(b64_profile + '=' * (-len(b64_profile) % 4))
    
    print(profile)