android谷歌日历api获取凭证

bxpogfeg  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(462)

这个问题在这里已经有答案了

尝试googlenethttptransport时未找到jks(1个答案)
googlenethttptransport.newtrustedtransport()返回null(2个答案)
19天前关门了。
我想开发一个应用程序,将访问谷歌日历api等。
应用程序的用户也应该能够访问这些事件。但不幸的是我不能用我的代码访问。我还遵循了googlecalendarapi-javaquickstart的说明。
这是我的密码:

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 我在另一个类中调用了get all data方法:

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

如果您有任何信息丢失的问题,请让我知道,我会把信息传递给您。
先谢谢你。

pb3s4cty

pb3s4cty1#

这是一个已知的问题,我想你可以在谷歌问题跟踪找到答案。
把这个换掉

HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

用这个

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

相关问题