Search code examples
javaoauth-2.0google-oauth

Gmail with Oauth2 in Java with refresh token with AuthorizationCodeFlow


I have an automatic mailing system whichs has a set of configured Gmail accounts. Google is forcing users to use Oauth for mailing so I created a new Client ID and Client Secrete from Google API Console. I've granted Gmail to access my account using a Python script, so I already have a refresh token.

My problem is that I'm trying to get a new access token using AuthorizationCodeFlow with that refresh token but I'm getting null:

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.TokenResponse;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.BasicAuthentication;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.util.store.MemoryDataStoreFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;

public class Main {
    public static void main(String[] args) throws IOException {
        String clientId = "748679640358-....b.apps.googleusercontent.com";
        String clientSecret = "tUI4ggLLexNc...MRM";
        String refreshToken = "1//0...........ARAAGBESNwF-L9Ir988VdRV3oiTYOQwygPjmwKw5r9Bi82q7JuoF2CysWw6xzW3z3Tda18GU5A_JMgdkGKw";

        List<String> scopes = Arrays.asList("https://mail.google.com/");
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                // Sends requests to the OAuth server
                new NetHttpTransport(),
                // Converts between JSON and Java
                JacksonFactory.getDefaultInstance(),
                // Your OAuth client ID
                clientId,
                // Your OAuth client secret
                clientSecret,
                // Tells the user what permissions they're giving you
                scopes)
                // Stores the user's credential in memory
                .setDataStoreFactory(MemoryDataStoreFactory.getDefaultInstance()).build();

        Credential credential;
        try {
            credential = flow.loadCredential(clientId);
        } catch (IOException e) {
            // Error getting login status
            credential = null;
        }
        
        
        if (credential == null) {
            TokenResponse tokenResponse = new TokenResponse().setRefreshToken(refreshToken);
            credential = flow.createAndStoreCredential(tokenResponse, clientId);
        } 
        
        String accessToken = credential.getAccessToken();
        
        System.out.println(accessToken); // THIS PRINTS null
    }
}

I don't know what I'm missing, any kind of help would be really appreciated


Solution

  • Like any tool developed with Java or Google, there is a lot of essential missing information. There's a refreshToken method (of course not mentioned in any of the tutorials and docs) which did the trick:

    ...
    if (credential == null) {
        TokenResponse tokenResponse = new TokenResponse().setRefreshToken(refreshToken);
        credential = flow.createAndStoreCredential(tokenResponse, clientId);
        credential.refreshToken();
    }
    ...
    

    I hope it prevents someone to lost a few days to make things work