将 Google 日历集成到 JSF Web 应用程序

问题描述 投票:0回答:1

我们的 Web 应用程序是用 JSF 开发的,允许用户在日历中安排会议。 目标是在应用程序设置中,每个用户都可以集成他们的 Google 帐户,以便应用程序中安排的会议可以记录在他们的 Google 日历中。

第一步是将 Google 帐户集成到应用程序的设置部分:

Calendar Settings

单击按钮会调用 backbean 中的函数 connectGoogleAcount,如代码所示:

public void connectGoogleAcount() throws IOException, GeneralSecurityException {
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
        .setApplicationName(APPLICATION_NAME).build();

System.out.printf("Account connected !");}

和函数 getCredentials:

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = CalendarQuickstart.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(USER_DIRECTORY_PATH + "/" + TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline").build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8080).build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    // returns an authorized Credential object.
    return credential;
}

单击按钮即可工作。在服务器上,我看到在浏览器中打开的 URL 是:

Please open the following address in your browser: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=00000000000000-yyyyyyyyyyyyyyyyyyddd.apps.googleusercontent.com&redirect_uri=http://localhost:80/Callback&response_type=code&scope=https://www.googleapis.com/auth/calendar.readonly

我的问题是如何让页面在点击按钮后自动在浏览器中打开?

java jsf google-oauth google-calendar-api google-api-java-client
1个回答
0
投票

您使用的代码是为已安装的应用程序设计的。它将在运行代码的机器上打开浏览器。这不适用于网络服务器。

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES)
            .setDataStoreFactory(
                    new FileDataStoreFactory(new java.io.File(USER_DIRECTORY_PATH + "/" + TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline").build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8080).build();
© www.soinside.com 2019 - 2024. All rights reserved.