简体   繁体   中英

I couldnt get jwt token from google

First of all i take client_id and client_secret from google developer tools. I am writing an API automation and I need to automatically get the Google token after signing in with a Google email and password in the option to sign in with Google on a site(eg: www.xyz.com ). First I added these dependencies to my pom.xml file

<dependency>
    <groupId>com.google.auth</groupId>
    <artifactId>google-auth-library-oauth2-http</artifactId>
    <version>1.12.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client -->
<dependency>
    <groupId>com.google.api-client</groupId>
    <artifactId>google-api-client</artifactId>
    <version>1.30.10</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-oauth2 -->
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-oauth2</artifactId>
    <version>v2-rev157-1.25.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client-jackson2 -->
<dependency>
    <groupId>com.google.api-client</groupId>
    <artifactId>google-api-client-jackson2</artifactId>
    <version>2.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
<dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.30.6</version>
</dependency>

After i wrote these codes:

package JWTToken;


import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.auth.oauth2.GoogleCredentials;

public class JwtToken {
    public static void main(String[] args) {
        // Google OAuth 2.0 
        String clientId = "519214871295-o8********3m9i75ao7mm.apps.googleusercontent.com";
        String clientSecret = "GOCSPX-*********7wKn5pXYZ";

        // Google OAuth 2.0 
        GoogleCredentials credentials = new GoogleCredentials.Builder()
                .setClientSecrets(clientId, clientSecret)
                .setTransport(new NetHttpTransport())
                .setJsonFactory(new JacksonFactory())
                .build();

        String accessToken = credentials.getAccessToken().getTokenValue();
        System.out.println(accessToken);
    }
}

But Builder() has protected access in com.google.auth.oauth2.GoogleCredentials.Builder . How can i access it or any advice for me. Thank you for your advice

I've found another way that would probably suit you better:

 /** Authorizes the installed application to access user's protected data. */
 private static Credential authorize() throws Exception {
   // load client secrets
   GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
       new InputStreamReader(CalendarSample.class.getResourceAsStream("/client_secrets.json")));
   // set up authorization code flow
   GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
       httpTransport, JSON_FACTORY, clientSecrets,
       Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory)
      .build();
   // authorize
   return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

See that documentation for more information.

But basically you should better store you clientId and clientSecret in a JSON file to have it not in the source code.

With that you can build your GoogleAuthorizationCodeFlow object and receive from that the Credential .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM