java中带有oauth2的gmail,带有带有authorizationcodeflow的刷新令牌

k75qkfdt  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(345)

我有一个自动邮件系统,它有一套配置好的gmail帐户。google正在强制用户使用oauth进行邮件发送,所以我从googleapi控制台创建了一个新的客户机id和客户机机密。我已经授权gmail使用python脚本访问我的帐户,所以我已经有了一个刷新令牌。
我的问题是,我正在尝试使用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
    }
}

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

yzxexxkh

yzxexxkh1#

像任何用java或google开发的工具一样,有很多极其重要的信息缺失。有一个refreshtoken方法(当然,在任何教程和文档中都没有提到)实现了这个技巧:

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

我希望这能防止有人浪费几天的时间来工作

相关问题