Java Google Drive API - Missing JsonFactory Method Error

I’m working on a Java application that needs to connect to Google Drive API. When I try to get the access token and refresh token for authentication, I keep running into this error:

java.lang.NoSuchMethodError: com.google.api.client.json.JsonFactory.fromInputStream(Ljava/io/InputStream;Ljava/lang/Class;)

I’m following the OAuth 2.0 authentication flow but it seems like I’m missing some required library or maybe using the wrong version. The error happens when my code tries to parse the JSON response from Google’s authentication server.

Here’s a basic example of what I’m trying to do:

GoogleAuthorizationCodeFlow authFlow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport,
    jsonFactory,
    clientSecrets,
    Collections.singleton(DriveScopes.DRIVE_FILE)
).build();

Credential userCredential = authFlow.loadCredential(userId);

Which specific JAR file contains the JsonFactory class with the fromInputStream method? I want to make sure I have all the right dependencies in my project.

had this exact problem last month! you’re missing the json implementatio jar. add google-http-client-jackson2 to your dependencies - that’s where fromInputStream actually lives. the main google-api-client doesn’t include everything for json parsing.

This happens when your Google API client libraries don’t match the JSON processing library you’re using. The fromInputStream method you need is in google-http-client-gson or google-http-client-jackson2 - not the main google-api-client library. You need the right JSON factory implementation in your classpath. I’ve seen this tons of times when people have multiple versions of Google client libraries or when Maven/Gradle pulls in conflicting dependencies. Check your dependency tree and make sure you’re not mixing old and new versions. Also double-check that your JsonFactory instance is initialized with the right implementation class.

I encountered a similar problem while working on a Google Drive API project recently. It turned out to be an issue with the library versions being used. To resolve the NoSuchMethodError for JsonFactory.fromInputStream, ensure that you are using compatible versions of google-api-client and google-http-client. Specifically, I suggest using google-api-client version 1.32.1 or higher along with google-http-client-jackson2 version 1.40.0 or higher. Additionally, if you are still using GsonFactory, consider switching to JacksonFactory, as certain methods have been relocated or deprecated in recent versions. Keep an eye on your project’s dependencies to avoid potential conflicts.