无法将我的桌面应用程序配置为Google Calendar API

时间:2014-02-11 14:03:33

标签: google-app-engine google-api google-calendar-api google-api-java-client google-api-client

我正在尝试设置我的项目以使用Google Calendar API。到目前为止,我已经下载了最新的库并导入了它们。目前,我正在尝试按照here找到的Google开发人员的教程进行操作。

根据我发现的link草案10已被弃用,我正在尝试使用其他课程 哪个不属于draft10。

以下是我目前的进口商品:

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.client.util.Lists;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.Calendar;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.api.services.calendar.model.Events;

以下是从Google示例中采用的方法,但有一些更改:

public void setUp() throws IOException {
        httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        // The clientId and clientSecret can be found in Google Developers Console
        String clientId = "YOUR_CLIENT_ID";
        String clientSecret = "YOUR_CLIENT_SECRET";

        // Or your redirect URL for web based applications.
        String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
        ArrayList<String> scopes = new ArrayList<String>();
        scopes.add("https://www.googleapis.com/auth/calendar");

        // Step 1: Authorize -->
        String authorizationUrl = new GoogleAuthorizationCodeRequestUrl(clientId, redirectUrl, scopes)
            .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 = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory,
            clientId, clientSecret, code, redirectUrl).execute();
        // End of Step 2 <--

        GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
            response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
            response.refreshToken);

        Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory);
        service.setApplicationName("YOUR_APPLICATION_NAME");
      }

唯一的问题是GoogleAccessProtectedResource类。它给了我以下错误:GoogleAccessProtectedResource cannot be resolved to a type

有没有人对如何解决这个问题有任何想法?

1 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。我所要做的就是导入以下包:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.PlusScopes;

并替换以下代码:

GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
            response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
            response.refreshToken);

Calendar service = new Calendar(httpTransport, accessProtectedResource, jsonFactory);
        service.setApplicationName("YOUR_APPLICATION_NAME");

使用此代码:

GoogleCredential credential;
credential = new GoogleCredential.Builder().setTransport(httpTransport)
    .setJsonFactory(jsonFactory).setServiceAccountId("[[INSERT SERVICE ACCOUNT EMAIL HERE]]")
    .setServiceAccountScopes(Collections.singleton(PlusScopes.PLUS_ME))
    .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
    .build();


Plus plus = new Plus.Builder(httpTransport, jsonFactory, credential)
    .setApplicationName("YOUR_APPLICATION_NAME")
    .build();