简体   繁体   中英

The type com.google.api.client.json.JsonFactory cannot be resolved

I am trying to implement Google Sign-In for server-side app. in my app engine application. I am using Eclipse Luna and I have followed the tutorial from this link: https://developers.google.com/identity/sign-in/web/server-side-flow#step_7_exchange_the_authorization_code_for_an_access_token

I have added the following jarfiles to my build path: google-api-client-jackson2-1.16.0-rc.jar , appengine-gcs-client-0.5.jar , and google-api-client-1.8.0-beta.jar which I downloaded from maven repo.

I have an error, and I don't know what else to do to resolve it. I have marked it in my code with double asterisks.

The error message is: The type com.google.api.client.json.JsonFactory cannot be resolved. It is indirectly referenced from required.class files. The type com.google.api.client.json.JsonFactory cannot be resolved. It is indirectly referenced from required.class files.
I have searched for similar issues on StackOverflow but no answer I found solved this problem.

Please, can some one have pity on me and help me out. This feature is delaying my deployment. Thank you in advance.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // Set path to the Web application client_secret_*.json file you downloaded from the
    // Google Developers Console: https://console.developers.google.com/apis/credentials?project=_
    // You can also find your Web application client ID and client secret from the
    // console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
    // object.
    ServletContext context = getServletContext();
    String CLIENT_SECRET_FILE  = context.getRealPath("/WEB-INF/client/client_secret.json");     

    // Exchange auth code for access token
    GoogleClientSecrets clientSecrets =
        **GoogleClientSecrets.load(
            JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));**
    GoogleTokenResponse tokenResponse =
              new GoogleAuthorizationCodeTokenRequest(
                  new NetHttpTransport(),
                  JacksonFactory.getDefaultInstance(),
                  "https://www.googleapis.com/oauth2/v4/token",
                  clientSecrets.getDetails().getClientId(),
                  clientSecrets.getDetails().getClientSecret(),
                  authCode,
                  REDIRECT_URI)  // Specify the same redirect URI that you use with your web
                                 // app. If you don't have a web version of your app, you can
                                 // specify an empty string.
                  .execute();

    String accessToken = tokenResponse.getAccessToken();

    // Use access token to call API
    GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
    Drive drive =
        new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
            .setApplicationName("Auth Code Exchange Demo")
            .build();
    File file = drive.files().get("appfolder").execute();

    // Get profile info from ID token
    GoogleIdToken idToken = tokenResponse.parseIdToken();
    GoogleIdToken.Payload payload = idToken.getPayload();
    String userId = payload.getSubject();  // Use this value as a key to identify a user.
    String email = payload.getEmail();
    boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
    String name = (String) payload.get("name");
    String pictureUrl = (String) payload.get("picture");
    String locale = (String) payload.get("locale");
    String familyName = (String) payload.get("family_name");
    String givenName = (String) payload.get("given_name");      

}
  1. In POM add below dependency-

     <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client-gson</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>com.google.oauth-client</groupId> <artifactId>google-oauth-client-jetty</artifactId> <version>1.34.1</version> </dependency>

Note: In above dependency version can be changed

2.Import statement

    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.gson.GsonFactory;

3.Usage

    private static final JsonFactory JSON_FACTORY = 
           GsonFactory.getDefaultInstance();
    GoogleClientSecrets clientSecrets =GoogleClientSecrets.load(JSON_FACTORY, 
             new InputStreamReader(in));

I assume that you have download the denpendencies but you don´t use maven, i think that the problem is that Maven resolves automaticaly the indirect dependencies, but you haven´t solve them.

If you want to use google´s jsonFactory you need the http-client library.

You can download it here :

http://repo2.maven.org/maven2/com/google/http-client/google-http-client-jackson/

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