【问题标题】:Android Google Calendar API get credentials [duplicate]Android Google Calendar API 获取凭据 [重复]
【发布时间】:2021-03-31 03:11:28
【问题描述】:

我想开发一个可以访问 Google 日历 API 的应用程序。

应用程序的用户也应该能够访问这些事件。 但不幸的是,我无法使用我的代码访问。我也按照Google Calendar API Java Quickstart的指示进行操作。

这是我的代码:

public class GoogleCalendar {

    private static final String APPLICATION_NAME = "Google Calendar 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 saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "/credential.json";

    /**
     * Creates an authorized Credential objects.
     * @param HTTP_TRANSPORT The network HTTP Transport
     * @return An authorized Credential object.
     * @throws IOException If the client_id.jason file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = GoogleCalendar.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 getAllData() throws GeneralSecurityException, IOException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
        // List the next 10 event from the primary calendar.
        DateTime now = new DateTime(System.currentTimeMillis());
        Events events = service.events().list("primary")
                .setMaxResults(10)
                .setTimeMin(now)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();
        List<Event> items = events.getItems();
        if (items.isEmpty()) {
            System.out.println("No upcoming events found.");
        } else {
            System.out.println("Upcoming events");
            for (Event event : items) {
                DateTime start = event.getStart().getDateTime();
                if (start == null) {
                    start = event.getStart().getDate();
                }
                System.out.printf("%s (%s)\n", events.getSummary(), start);
            }
        }
    }
}

这是我的错误信息: java.security.KeyStoreException: JKS not found

我在另一个类中调用了获取所有数据方法:

try {
    GoogleCalendar.getAllData();
} catch (GeneralSecurityException | IOException e) {
    e.printStackTrace();
}

如果您对信息丢失有任何疑问,请告诉我,我会传递信息。

提前谢谢你。

【问题讨论】:

    标签: java android google-api google-calendar-api


    【解决方案1】:

    这是一个已知问题,我认为您可以在 google issue tracker 上找到答案。

    只需替换这个

    HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    

    有了这个

    HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport()
    

    【讨论】:

    • 请不要复制现有答案并重复使用它们,只需复制问题即可。
    • @DaImTo 我在讨论这个话题时遇到了同样的问题。在那一刻,我遇到了这个链接,我仍然收藏了书签。如果在此处给出此处复制和粘贴,我还没有寻找其他相同的答案。每个看起来相同的答案都不会被复制粘贴。
    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2012-10-14
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多