Google api 如何使用gmail api调用在java中处理身份验证令牌流

Google api 如何使用gmail api调用在java中处理身份验证令牌流,google-api,google-oauth,gmail-api,google-api-java-client,Google Api,Google Oauth,Gmail Api,Google Api Java Client,.我想使用myApp集成整个gmail功能,如阅读、列表、发送。我尝试过使用HTTP/1.1 但无法获取访问令牌。请提供流量 已尝试使用POST:HTTP/1.1和POST:Through,但未找到错误404。您可能需要查阅它包含完整的授权示例 import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCod

.我想使用myApp集成整个gmail功能,如阅读、列表、发送。我尝试过使用HTTP/1.1 但无法获取访问令牌。请提供流量

已尝试使用POST:HTTP/1.1和POST:Through,但未找到错误404。

您可能需要查阅它包含完整的授权示例

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;
导入com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
导入com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
导入com.google.api.client.googleapis.auth.oauth2.googleaauthorizationcodeflow;
导入com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
导入com.google.api.client.http.javanet.NetHttpTransport;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.json.jackson2.JacksonFactory;
导入com.google.api.client.util.store.FileDataStoreFactory;
导入com.google.api.services.gmail.gmail;
导入com.google.api.services.gmail.GmailScopes;
导入com.google.api.services.gmail.model.Label;
导入com.google.api.services.gmail.model.ListLabelsResponse;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.security.GeneralSecurityException;
导入java.util.Collections;
导入java.util.List;
公共类Gmail快速入门{
私有静态最终字符串应用程序\u NAME=“Gmail API Java Quickstart”;
私有静态最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
私有静态最终字符串令牌\u目录\u路径=“令牌”;
/**
*此快速启动所需作用域的全局实例。
*如果修改这些作用域,请删除以前保存的令牌/文件夹。
*/
私有静态最终列表范围=Collections.singletonList(GmailScopes.GMAIL_标签);
私有静态最终字符串凭据文件路径=“/CREDENTIALS.json”;
/**
*创建授权凭据对象。
*@param HTTP_传输网络HTTP传输。
*@返回授权凭证对象。
*@在找不到credentials.json文件时引发IOException。
*/
私有静态凭据getCredentials(最终NetHttpTransport HTTP_传输)引发IOException{
//加载客户端机密。
InputStream in=GmailQuickstart.class.getResourceAsStream(凭证文件路径);
if(in==null){
抛出新的FileNotFoundException(“未找到资源:+凭证\文件\路径”);
}
GoogleClientSecrets clientSecrets=GoogleClientSecrets.load(JSON_工厂,新的InputStreamReader(in));
//生成流并触发用户授权请求。
GoogleAuthorizationCodeFlow=新建GoogleAuthorizationCodeFlow.Builder(
HTTP_传输、JSON_工厂、客户端机密、作用域)
.setDataStoreFactory(新的FileDataStoreFactory(新的java.io.File(令牌\目录\路径)))
.setAccessType(“脱机”)
.build();
LocalServerReceiver=新的LocalServerReceiver.Builder().setPort(8888.build();
返回新的AuthorizationCodeInstalledApp(流程、接收方)。授权(“用户”);
}
publicstaticvoidmain(字符串…参数)抛出IOException、GeneralSecurityException{
//构建新的授权API客户端服务。
最终NetHttpTransport HTTP_TRANSPORT=GoogleNetHttpTransport.newTrustedTransport();
Gmail服务=new Gmail.Builder(HTTP_传输、JSON_工厂、getCredentials(HTTP_传输))
.setApplicationName(应用程序名称)
.build();
//打印用户帐户中的标签。
字符串user=“me”;
ListLabelsResponse listResponse=service.users().labels().list(user.execute();
List labels=listResponse.getLabels();
if(labels.isEmpty()){
System.out.println(“未找到标签”);
}否则{
System.out.println(“标签:”);
用于(标签:标签){
System.out.printf(“-%s\n”,label.getName());
}
}
}
}
应用验证
记住,gmail示波器被认为是最敏感的。您应该考虑提交您的应用程序现在验证,因为它可能需要时间来完成这个过程。还要记住,你可能会因为验证gmail范围而被收取费用,因为审查是由第三方完成的,而不是谷歌自己

我不明白你的解决办法。我想使用令牌有办法吗@DaImTo@Sa1令牌在代码中进行处理,答案中提供的快速启动是您使用令牌设置代码所需的,您不需要securetoken.googleapis.com/v1/tokenSecure token是firebase的一部分。您应该使用官方的google客户端库来处理此api。请尝试遵循快速入门。请编辑您的问题并包含您的代码,以便我们可以尝试重新打开它。