Gmail API:Gmail API 的凭据

3
这是"Java Quickstart"教程中提供的与Gmail API相关的代码。以下是我需要执行的步骤以创建应用程序凭据:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
    .setAccessType("online")
    .setApprovalPrompt("auto").build();

String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
    .build();
System.out.println("Please open the following URL in your browser then type"
    + " the authorization code:\n" + url);

// Read code entered by user.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = null;
try {
   code = br.readLine();
} catch (IOException e) {
   e.printStackTrace();
}

// Generate Credential using retrieved code.
GoogleTokenResponse response = null;
try {
   response = flow.newTokenRequest(code)
           .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
} catch (IOException e) {
   e.printStackTrace();
}
GoogleCredential credential = new GoogleCredential()
    .setFromTokenResponse(response);


有什么方法可以自动化上述过程,就像在此处获取凭据以供进一步使用一样?

以下示例是针对Google任务的。

GoogleAccountCredential credential =
   GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));
2个回答

1
GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(service_account)
            .setServiceAccountScopes(
                    Collections.singleton("https://mail.google.com/"))
            // .setServiceAccountPrivateKeyFromP12File(new
            // File(certLocation))
            .setServiceAccountPrivateKey(serviceAccountPrivateKey)
            .setServiceAccountUser(senderid).build();

1
在一个目录中存储此应用程序的用户凭据。
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开发者控制台下载并存储client_secrets.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,您的应用程序就不会请求权限。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接