Google Calendar API - 获取Web应用程序的访问令牌

时间:2015-02-12 18:07:56

标签: java api calendar

我使用了以下代码。在这里,我可以通过readline获取令牌。如何从URL访问令牌。请帮我。我是API中的新人。

这是我的代码

    try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        //String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
        String scope = "https://www.googleapis.com/auth/calendar";

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
                httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Collections.singleton(scope));
            // Step 1: Authorize
            String authorizationUrl = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();

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

            // Read the authorization code from the standard input stream.
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What is the authorization code?");
            String code = in.readLine();
            // End of Step 1

            // Step 2: Exchange
            GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI)
                .execute();
            // End of Step 2

            Credential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
                .build().setFromTokenResponse(response);

            Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credential)
                .setApplicationName("Minutes Of Meeting").build();

            String pageToken = null;
            do {
              Events events = service.events().list("primary").setPageToken(pageToken).execute();
              List<Event> items = events.getItems();
              for (Event event : items) {
                System.out.println(event.getSummary());
              }
              pageToken = events.getNextPageToken();
            } while (pageToken != null);

这很好用。我应该在GAE中推送此代码吗?没有任何变化?它有效吗?

0 个答案:

没有答案