【问题标题】:Cant configure my desktop application to Google Calendar API无法将我的桌面应用程序配置为 Google Calendar API
【发布时间】:2014-02-11 14:03:32
【问题描述】:

我正在尝试将我的项目设置为使用 Google 日历 API。到目前为止,我已经下载了最新的库并导入了它们。目前,我正在尝试遵循来自 Google 开发人员的教程,该教程位于 here

根据我发现的linkdraft10 已被弃用,我正在尝试使用其他类 不属于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

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

【问题讨论】:

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


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 2022-09-23
      • 1970-01-01
      • 2011-03-30
      相关资源
      最近更新 更多