Java 使用Gmail QuickStart时无法通过获取google api令牌

Java 使用Gmail QuickStart时无法通过获取google api令牌,java,google-api,gmail-api,google-api-java-client,Java,Google Api,Gmail Api,Google Api Java Client,我已经从下载了一个gmail快速入门演示 我已经在谷歌云平台上注册,启用了gmail api,创建了我自己的项目和凭证 当我想到一切都在正确的方向上。下一秒,我陷入了困境 第一次,我运行的网站提供的原始程序 import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalled

我已经从下载了一个gmail快速入门演示

我已经在谷歌云平台上注册,启用了gmail api,创建了我自己的项目和凭证

当我想到一切都在正确的方向上。下一秒,我陷入了困境

第一次,我运行的网站提供的原始程序


    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.client.util.store.FileDataStoreFactory;
    import com.google.api.services.gmail.Gmail;
    import com.google.api.services.gmail.GmailScopes;
    import com.google.api.services.gmail.model.Label;
    import com.google.api.services.gmail.model.ListLabelsResponse;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.security.GeneralSecurityException;
    import java.util.Collections;
    import java.util.List;
    
    public class GmailQuickstart {
        private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
        private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        private static final String TOKENS_DIRECTORY_PATH = "tokens";
    
        /**
         * Global instance of the scopes required by this quickstart.
         * If modifying these scopes, delete your previously saved tokens/ folder.
         */
        private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
        private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
    
        /**
         * Creates an authorized Credential object.
         * @param HTTP_TRANSPORT The network HTTP Transport.
         * @return An authorized Credential object.
         * @throws IOException If the credentials.json file cannot be found.
         */
        private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
            // Load client secrets.
            InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
            if (in == null) {
                throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
            }
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    
            // Build flow and trigger user authorization request.
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                    .setAccessType("offline")
                    .build();
            LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
            return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        }
    
        public static void main(String... args) throws IOException, GeneralSecurityException {
            // Build a new authorized API client service.
            final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                    .setApplicationName(APPLICATION_NAME)
                    .build();
    
            // Print the labels in the user's account.
            String user = "me";
            ListLabelsResponse listResponse = service.users().labels().list(user).execute();
            List<Label> labels = listResponse.getLabels();
            if (labels.isEmpty()) {
                System.out.println("No labels found.");
            } else {
                System.out.println("Labels:");
                for (Label label : labels) {
                    System.out.printf("- %s\n", label.getName());
                }
            }
        }
    }

在我的谷歌搜索之后,我发现这可能是一个代理问题。然后我重写程序如下,添加了代理配置


    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.GoogleUtils;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
    import com.google.api.client.http.javanet.ConnectionFactory;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.client.util.store.FileDataStoreFactory;
    import com.google.api.services.gmail.Gmail;
    import com.google.api.services.gmail.GmailScopes;
    import com.google.api.services.gmail.model.Label;
    import com.google.api.services.gmail.model.ListLabelsResponse;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    import java.security.GeneralSecurityException;
    import java.util.Collections;
    import java.util.List;
    
    public class GmailQuickstart3 {
        private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
        private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        private static final String TOKENS_DIRECTORY_PATH = "tokens";
    
        /**
         * Global instance of the scopes required by this quickstart.
         * If modifying these scopes, delete your previously saved tokens/ folder.
         */
        private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
        private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
    
        /**
         * Creates an authorized Credential object.
         * @param HTTP_TRANSPORT The network HTTP Transport.
         * @return An authorized Credential object.
         * @throws IOException If the credentials.json file cannot be found.
         */
        private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
            // Load client secrets.
            InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
            if (in == null) {
                throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
            }
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    
            // Build flow and trigger user authorization request.
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                    .setAccessType("offline")
                    .build();
            LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
            return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        }
    
        public static void main(String... args) throws IOException, GeneralSecurityException {
            // Build a new authorized API client service.
            System.out.println(System.getProperties());
    
            final NetHttpTransport HTTP_TRANSPORT =
                    (new NetHttpTransport.Builder())
                            .trustCertificates(GoogleUtils.getCertificateTrustStore())
                            .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("******", 30900)))
    //                         .setConnectionFactory(connectionFactory)
                            .build();
            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)).setApplicationName(APPLICATION_NAME).build();
    
            // Print the labels in the user's account.
            String user = "me";
            ListLabelsResponse listResponse = service.users().labels().list(user).execute();
            List<Label> labels = listResponse.getLabels();
            if (labels.isEmpty()) {
                System.out.println("No labels found.");
            } else {
                System.out.println("Labels:");
                for (Label label : labels) {
                    System.out.printf("- %s\n", label.getName());
                }
            }
        }
    }
    ```
    Then I have got
    ```
    Please open the following address in your browser:
      https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=2467719584-um6onu5chc8hmn00is5j3ctl6g2mvvru.apps.googleusercontent.com&redirect_uri=http://localhost:8888/Callback&response_type=code&scope=https://www.googleapis.com/auth/gmail.labels
    Attempting to open that address in the default browser now...
    Exception in thread "main" java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 301 Moved Permanently"
        at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2152)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1340)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1315)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:264)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:108)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:79)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:995)
        at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:322)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
        at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:127)
        at GmailQuickstart3.getCredentials(GmailQuickstart3.java:63)
        at GmailQuickstart3.main(GmailQuickstart3.java:88)

有人能帮我看一下吗? 我真的很困惑,问题已经解决了好几天了,我还没有找到任何有用的文档

梯度运行结果:(于2020年10月9日添加)

PS D:\xnj\workspace\gmailQuickstart> gradle run

> Task :run
十月 09, 2020 11:07:16 下午 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
警告: unable to change permissions for everybody: D:\xnj\workspace\gmailQuickstart\tokens IDLE
十月 09, 2020 11:07:16 下午 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
警告: unable to change permissions for owner: D:\xnj\workspace\gmailQuickstart\tokens
2020-10-09 23:07:16.074:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2020-10-09 23:07:16.075:INFO::jetty-6.1.26
2020-10-09 23:07:16.088:INFO::Started SocketConnector@localhost:8888
Please open the following address in your browser:
  https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=2467719584-um6onu5chc8hmn00is5j3ctl6g2mvvru.apps.googleusercontent.com&redirect_uri=http://localhost:8888/Callback&response_type=code&scope=https://www.googleapis.com/auth/gmail.labels
Attempting to open that address in the default browser now...
2020-10-09 23:08:54.694:INFO::Stopped SocketConnector@localhost:8888
Exception in thread "main" java.net.SocketTimeoutException: connect timed out
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
        at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
        at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:84)
        at GmailQuickstart.getCredentials(GmailQuickstart.java:57)
        at GmailQuickstart.main(GmailQuickstart.java:63)

> Task :run FAILED
PS D:\xnj\workspace\gmailQuickstart>gradle run
>任务:运行
十月 09, 2020 11:07:16 下午 com.google.api.client.util.store.FileDataStoreFactory设置权限
警告: 无法更改每个人的权限:D:\xnj\workspace\gmailQuickstart\tokens IDLE
十月 09, 2020 11:07:16 下午 com.google.api.client.util.store.FileDataStoreFactory设置权限
警告: 无法更改所有者的权限:D:\xnj\workspace\gmailQuickstart\tokens
2020-10-09 23:07:16.074:信息::通过org.mortbay.log.StdErrLog记录到STDERR
2020-10-09 23:07:16.075:信息::jetty-6.1.26
2020-10-09 23:07:16.088:信息::开始SocketConnector@localhost:8888

请在浏览器中打开以下地址: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=2467719584-um6onu5chc8hmn00is5j3ctl6g2mvvru.apps.googleusercontent.com&重定向uri=http://localhost:8888/Callback&response_type=code&scope=https://www.googleapis.com/auth/gmail.labels 正在尝试在默认浏览器中打开该地址。。。 2020-10-09 23:08:54.694:信息::已停止SocketConnector@localhost:8888 线程“main”java.net.SocketTimeoutException中出现异常:连接超时 位于java.net.DualStackPlainSocketImpl.waitForConnect(本机方法) 位于java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) 位于java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 位于java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 位于java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 位于java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 位于java.net.socksocketimpl.connect(socksocketimpl.java:392) 位于java.net.Socket.connect(Socket.java:589) 位于sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673) 位于sun.net.NetworkClient.doConnect(NetworkClient.java:175) 位于sun.net.www.http.HttpClient.openServer(HttpClient.java:463) 位于sun.net.www.http.HttpClient.openServer(HttpClient.java:558) 在sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:264) 位于sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) 位于sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156) 位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) 位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) 位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334) 位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309) 位于sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259) 位于com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77) 位于com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981) 位于com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283) 在com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158) 在com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79) 位于com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorization(AuthorizationCodeInstalledApp.java:84) 在GmailQuickstart.getCredentials(GmailQuickstart.java:57) 在GmailQuickstart.main(GmailQuickstart.java:63) >任务:运行失败
在我单击控制台中的url之前,控制台的日志将挂起在“现在尝试在默认浏览器中打开该地址…”,如果我不做任何操作,它将保持原样。当我单击url时,控制台将立即打印错误跟踪,并告诉我套接字“连接超时”


请在您的浏览器中打开以下地址:我无法复制此地址,快速启动工作正常。所以您尝试通过浏览器访问提供的URL?然后发生了什么?是的,我已经访问了提供的链接,它让我选择一个谷歌帐户,我只是这样做。我得到了:收到的验证码。您现在可以关闭此窗口。broswer url更改为:您是否按照快速入门的原样进行操作?使用gradle等,您可以尝试使用其他浏览器打开URL吗?你注意到有什么变化吗?刚才,我用gradle运行这个程序,但还是失败了。日志追加在上面,请查看“梯度运行结果”

    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.GoogleUtils;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
    import com.google.api.client.http.javanet.ConnectionFactory;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.client.util.store.FileDataStoreFactory;
    import com.google.api.services.gmail.Gmail;
    import com.google.api.services.gmail.GmailScopes;
    import com.google.api.services.gmail.model.Label;
    import com.google.api.services.gmail.model.ListLabelsResponse;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.URL;
    import java.security.GeneralSecurityException;
    import java.util.Collections;
    import java.util.List;
    
    public class GmailQuickstart3 {
        private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
        private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
        private static final String TOKENS_DIRECTORY_PATH = "tokens";
    
        /**
         * Global instance of the scopes required by this quickstart.
         * If modifying these scopes, delete your previously saved tokens/ folder.
         */
        private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
        private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
    
        /**
         * Creates an authorized Credential object.
         * @param HTTP_TRANSPORT The network HTTP Transport.
         * @return An authorized Credential object.
         * @throws IOException If the credentials.json file cannot be found.
         */
        private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
            // Load client secrets.
            InputStream in = GmailQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
            if (in == null) {
                throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
            }
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    
            // Build flow and trigger user authorization request.
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                    .setAccessType("offline")
                    .build();
            LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
            return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        }
    
        public static void main(String... args) throws IOException, GeneralSecurityException {
            // Build a new authorized API client service.
            System.out.println(System.getProperties());
    
            final NetHttpTransport HTTP_TRANSPORT =
                    (new NetHttpTransport.Builder())
                            .trustCertificates(GoogleUtils.getCertificateTrustStore())
                            .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("******", 30900)))
    //                         .setConnectionFactory(connectionFactory)
                            .build();
            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)).setApplicationName(APPLICATION_NAME).build();
    
            // Print the labels in the user's account.
            String user = "me";
            ListLabelsResponse listResponse = service.users().labels().list(user).execute();
            List<Label> labels = listResponse.getLabels();
            if (labels.isEmpty()) {
                System.out.println("No labels found.");
            } else {
                System.out.println("Labels:");
                for (Label label : labels) {
                    System.out.printf("- %s\n", label.getName());
                }
            }
        }
    }
    ```
    Then I have got
    ```
    Please open the following address in your browser:
      https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=2467719584-um6onu5chc8hmn00is5j3ctl6g2mvvru.apps.googleusercontent.com&redirect_uri=http://localhost:8888/Callback&response_type=code&scope=https://www.googleapis.com/auth/gmail.labels
    Attempting to open that address in the default browser now...
    Exception in thread "main" java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 301 Moved Permanently"
        at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2152)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1340)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1315)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:264)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:108)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:79)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:995)
        at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:322)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
        at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:127)
        at GmailQuickstart3.getCredentials(GmailQuickstart3.java:63)
        at GmailQuickstart3.main(GmailQuickstart3.java:88)

CONNECT oauth2.googleapis.com:443 HTTP/1.1
User-Agent: Java/1.8.0_231
Host: oauth2.googleapis.com
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Proxy-Connection: keep-alive

HTTP/1.1 301 Moved Permanently
Content-length: 0
Location: https://oauth2.googleapis.com/
PS D:\xnj\workspace\gmailQuickstart> gradle run

> Task :run
十月 09, 2020 11:07:16 下午 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
警告: unable to change permissions for everybody: D:\xnj\workspace\gmailQuickstart\tokens IDLE
十月 09, 2020 11:07:16 下午 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
警告: unable to change permissions for owner: D:\xnj\workspace\gmailQuickstart\tokens
2020-10-09 23:07:16.074:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2020-10-09 23:07:16.075:INFO::jetty-6.1.26
2020-10-09 23:07:16.088:INFO::Started SocketConnector@localhost:8888
Please open the following address in your browser:
  https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=2467719584-um6onu5chc8hmn00is5j3ctl6g2mvvru.apps.googleusercontent.com&redirect_uri=http://localhost:8888/Callback&response_type=code&scope=https://www.googleapis.com/auth/gmail.labels
Attempting to open that address in the default browser now...
2020-10-09 23:08:54.694:INFO::Stopped SocketConnector@localhost:8888
Exception in thread "main" java.net.SocketTimeoutException: connect timed out
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:673)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1334)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1309)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:259)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
        at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
        at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:84)
        at GmailQuickstart.getCredentials(GmailQuickstart.java:57)
        at GmailQuickstart.main(GmailQuickstart.java:63)

> Task :run FAILED