首页 > 解决方案 > 在 Java 中使用 Oauth2 的 Gmail 以及带有 AuthorizationCodeFlow 的刷新令牌

问题描述

我有一个自动邮件系统,它有一组配置的 Gmail 帐户。Google 强制用户使用 Oauth 发送邮件,所以我从Google API Console创建了一个新的 Client ID 和 Client Secrete 。我已经授权 Gmail 使用Python 脚本访问我的帐户,所以我已经有了一个刷新令牌

我的问题是我正在尝试使用带有刷新令牌的AuthorizationCodeFlow获取新的访问令牌,但我得到了空值:

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
    }
}

我不知道我错过了什么,任何帮助都会非常感激

标签: javaoauth-2.0google-oauth

解决方案


与使用 Java 或 Google 开发的任何工具一样,存在许多极其重要的缺失信息。有一个refreshToken方法(当然在任何教程和文档中都没有提到),它成功了:

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

我希望它可以防止有人浪费几天时间来使事情顺利进行


推荐阅读