Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

oauth 2.0 - 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

question from:https://stackoverflow.com/questions/65600737/gmail-with-oauth2-in-java-with-refresh-token-with-authorizationcodeflow

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

I hope it prevent someone to lost few days to make things work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...