简体   繁体   English

Gmail 在 Java 中带有 Oauth2,带有带有 AuthorizationCodeFlow 的刷新令牌

[英]Gmail with Oauth2 in Java with refresh token with AuthorizationCodeFlow

I have an automatic mailing system whichs has a set of configured Gmail accounts.我有一个自动邮件系统,它有一组配置的 Gmail 帐户。 Google is forcing users to use Oauth for mailing so I created a new Client ID and Client Secrete from Google API Console . Google 强制用户使用 Oauth 进行邮件发送,所以我从Google API Console创建了一个新的 Client ID 和 Client Secrete。 I've granted Gmail to access my account using a Python script , so I already have a refresh token .我已经授权 Gmail 使用Python 脚本访问我的帐户,所以我已经有了一个刷新令牌

My problem is that I'm trying to get a new access token using AuthorizationCodeFlow with that refresh token but I'm getting null:我的问题是我正在尝试使用带有刷新令牌的AuthorizationCodeFlow获取新的访问令牌,但我得到的是 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我不知道我错过了什么,任何帮助都会非常感激

Like any tool developed with Java or Google there is a lot of extremely important missing information.与使用 Java 或 Google 开发的任何工具一样,存在许多极其重要的缺失信息。 There's a refreshToken method (of course not mentioned in any of the tutorials and docs) which made the trick:有一个refreshToken方法(当然在任何教程和文档中都没有提到),它成功了:

...
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我希望它可以防止有人浪费几天时间来使事情顺利进行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM