Gmail OAuth Authentication Failing with Invalid Credentials Error in Java

I’m working on a Java application that needs to connect to Gmail using OAuth authentication but I keep getting an “Invalid Credentials” error. I’ve been stuck on this for a while and can’t figure out what’s wrong.

Here’s my current implementation:

public class GmailAuthenticator {

    private static final String EMAIL_SCOPE = "https://mail.google.com/";
    private static final String CLIENT_KEY = "your_client_key";
    private static final String CLIENT_SECRET = "your_client_secret";
    private static final String GMAIL_ADDRESS = "[email protected]";

    public static void main(String[] args) throws Exception {
        
        GoogleOAuthParameters authParams = new GoogleOAuthParameters();
        authParams.setOAuthConsumerKey(CLIENT_KEY);
        authParams.setOAuthConsumerSecret(CLIENT_SECRET);
        
        OAuthSigner hmacSigner = new OAuthHmacSha1Signer();
        GoogleOAuthHelper authHelper = new GoogleOAuthHelper(hmacSigner);
        authParams.setScope(EMAIL_SCOPE);
        
        authHelper.getUnauthorizedRequestToken(authParams);
        String authUrl = authHelper.createUserAuthorizationUrl(authParams);
        
        System.out.println("Visit this URL: " + authUrl);
        System.out.println("Press enter after authorization...");
        System.in.read();
        
        String accessToken = authHelper.getAccessToken(authParams);
        System.out.println("Access Token: " + accessToken);
        
        XoauthAuthenticator.initialize();
        IMAPSSLStore store = XoauthAuthenticator.connectToImap(
            "imap.gmail.com",
            993,
            GMAIL_ADDRESS,
            authParams.getOAuthToken(),
            authParams.getOAuthTokenSecret(),
            new OAuthConsumer(null, CLIENT_KEY, CLIENT_SECRET, null),
            true
        );
        
        System.out.println("IMAP connection successful");
    }
}

The error I’m getting is:

A1 NO [ALERT] Invalid credentials (Failure)
javax.mail.AuthenticationFailedException: [ALERT] Invalid credentials (Failure)

I’ve tried using real consumer credentials but still get the same error. My goal is to programmatically access Gmail after user authorization to read emails for processing. Has anyone encountered this issue before? What could be causing the authentication to fail?

Had the exact same issue about a year ago. You’re using the old OAuth 1.0 flow - that’s your problem. Google killed OAuth 1.0 support for Gmail a while back, so even correct keys won’t work anymore. You need to switch to OAuth 2.0 with the Google API Client Library for Java. It’s completely different - instead of request tokens and signatures, you’ll download a credentials.json file from Google Cloud Console and work with authorization codes and refresh tokens. Don’t forget to enable the Gmail API in your project and set up your OAuth consent screen properly. Your scope should be “https://www.googleapis.com/auth/gmail.readonly” instead of that old mail.google.com one you’re using.

Your code’s using OAuth 1.0 classes that Google killed off years ago. I hit the same authentication issues when I had to migrate some old apps last year. Those GoogleOAuthParameters and OAuthHmacSha1Signer classes you’re importing? They’re from the ancient GData library that stopped working for Gmail back in 2012. You need to scrap all that and rebuild with the modern Google APIs Client Library for Java using OAuth 2.0. Set up a new project in Google Cloud Console, flip on the Gmail API, grab the client config JSON, and swap to GoogleAuthorizationCodeFlow. Way simpler once you’re on the current libraries. Double-check your redirect URIs match exactly what’s in your console too.

oauth 1.0 doesn’t work anymore - google killed it for gmail over 10 years ago. those old classes won’t get you anywhere. you need oauth 2.0 instead. grab the google-api-services-gmail dependency and use googlecredentials with the installed app flow. much easier once you make the switch.