Thai Rodrigues
Thai Rodrigues

Reputation: 301

Token from Google Credentials Service Account NULL

I am attempting to retrieve the credentials for my Google service account with the following code:

package function

import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.LambdaLogger
import com.amazonaws.services.lambda.runtime.RequestHandler
import pojo.Request

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.services.sqladmin.SQLAdminScopes
import pojo.Response



class GoogleAuth implements RequestHandler<Request, Response> {

    private LambdaLogger logger

    @Override
    Response handleRequest(Request input, Context context) {

        logger = context.getLogger()
        ClassLoader classLoader = getClass().getClassLoader()
        File jsonCredentials = new File(classLoader.getResource("leads-cloud-function-service-account.json").getFile())
        FileInputStream fis = new FileInputStream(jsonCredentials)


        GoogleCredential credential = GoogleCredential.fromStream(fis).createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))

        credential.getClientAuthentication()

        println("Thai test" + credential.getExpirationTimeMilliseconds())
        print("This is the credential" + credential.getAccessToken())

        return (new Response())

    }
}

The information about the milliseconds for expiration and the AccessToken are null. Has anyone ever experienced this?

Upvotes: 1

Views: 813

Answers (1)

Thai Rodrigues
Thai Rodrigues

Reputation: 301

So I have resolved this myself by adding before the getToken the following command:

credential.refreshToken()

This is not properly documented in google's website but after adding it, this worked perfectly and the results are displayed with actual values.

Some other error I faced was the "com.google.common.io.ByteStreams.exhaust(Ljava/io/InputStream;)J", in order to resolve it I had to correct the dependency I was using to this one:

<dependency>
 <groupId>com.google.api-client</groupId>
 <artifactId>google-api-client</artifactId>
 <version>1.31.1</version>
</dependency>

so, keep on mind that too!

Upvotes: 1

Related Questions