【问题标题】:Java - GMail API - Authorization codeJava - GMail API - 授权码
【发布时间】:2016-08-18 09:54:55
【问题描述】:

我正在尝试下面的代码。控制台打印出一个 URL(在我的浏览器地址栏中粘贴后)将我发送到 google 的用户同意页面并请求访问我的帐户的权限。然后它会将我重定向到我的 html 页面 - 到目前为止一切顺利。

现在,我不确定我是否收到了令牌或授权码。我从哪里获得它?然后我是否必须从 Web 应用程序发送 HTTP 休息调用以配合 Gmail API 请求,或者我可以通过 JAVA 来完成吗?

public class People {
  public void setUp() throws IOException {
        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        String clientId = "client_id";
        String clientSecret = "secret";

        String redirectUrl = "http://localhost:8080/TestingGmailMail/webapps/login.html";
        String scope = "https://www.googleapis.com/auth/contacts.readonly";


        String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId,redirectUrl,Arrays.asList(scope)).build();


        // Point or redirect your user to the authorizationUrl.
        System.out.println("Go to the following link in your browser:");
        System.out.println(authorizationUrl);
        }
}

【问题讨论】:

  • 我不是 Java 专家,但客户端库应该会为您处理这一切 您是否尝试阅读本教程? developers.google.com/gmail/api/quickstart/java#prerequisites
  • 非常感谢 - DalmTo!是的,我试过阅读它,发现它非常迟钝。我会尝试浏览图书馆
  • 当您开始使用 Google API 时会感到非常困惑。我对 java 无能为力,但也许我可以帮助指导你一点。 GoogleAuthorizationCodeFlow 处理来自用户的请求身份验证,它保存身份验证(不知道在 java 中的哪个位置)应该是 refreshtoken 和访问令牌。 getGmailService 将授予您访问 API 的权限,它会在需要时获取新的访问令牌。您所做的一切都将通过 GmailService

标签: java google-oauth gmail-api google-api-java-client


【解决方案1】:

是的,@DalmTo 指出客户端库应该处理所有这些是正确的。

更好的设计是将应用程序的用户凭据存储在目录中。

private static final java.io.File DATA_STORE_DIR = new java.io.File(
    System.getProperty("user.home"), ".store/mail_credentials");

现在,为FileDataStoreFactory 创建一个全局实例。

private static FileDataStoreFactory DATA_STORE_FACTORY;

在获取凭据之前实例化DATA_STORE_FACTORY,最好在静态块中。

DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);

从 Google Developer 控制台下载并存储 client_secret.json。使用以下方法获取凭据:

public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in
            = GmailQuickStart.class.getResourceAsStream("/client_secrets.json");
    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(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

每当调用上述方法时,它都会在提供给DATA_STORE_DIR 的路径中查找StoredCredential。如果找到,则代码按原样执行。如果没有,将打开一个浏览器,要求您登录并授权您的应用程序。这样生成的凭据将存储在DATA_STORE_DIR 位置。只要StoredCredential 存在,您的应用就不会请求许可。这是一种通用设计,也可用于其他 Google API。

【讨论】:

    猜你喜欢
    • 2018-05-21
    • 2020-06-22
    • 1970-01-01
    • 2016-12-09
    • 2021-01-23
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多