2

I need some help for access tokens in GCP. I am using Java as program language and I tried different approaches like: https://cloud.google.com/iap/docs/authentication-howto and https://developers.google.com/identity/protocols/OAuth2ServiceAccount#jwt-auth

I am using the second approach. Code snippet:

 String privateKeyId = "my-private-key";

    long now = System.currentTimeMillis();

    String signedJwt = null;

    try {
        Algorithm algorithm = Algorithm.RSA256(null, privateKey);
         signedJwt = JWT.create()
                .withKeyId(privateKeyId)
                .withIssuer("my-issuer")
                .withSubject("my-subject")
             .withAudience("https://www.googleapis.com/compute/v1/compute.machineTypes.list")
                .withIssuedAt(new Date(now))
                .withExpiresAt(new Date(now + 3600 * 1000L))
                .sign(algorithm);
    } catch (Exception e){
        e.printStackTrace();
    }

    return signedJwt;

Then I perform get instances setting the returned token as Bearer authorization header but response is:

 com.google.api.client.http.HttpResponseException: 401 Unauthorized
     {
     "error": {
     "errors": [
       {
       "domain": "global",
       "reason": "authError",
       "message": "Invalid Credentials",
       "locationType": "header",
       "location": "Authorization"
       }
       ],
       "code": 401,
    "message": "Invalid Credentials"
       }
       }

With same credentials I am able to access the SDK.

Thanks!

user2739823
  • 397
  • 1
  • 7
  • 24

1 Answers1

0

As per @DalmTo, you should be using the client library for Java. This is the link to get you started.

simhumileco
  • 31,877
  • 16
  • 137
  • 115
dany L
  • 2,456
  • 6
  • 12