Question

I am trying to set up my project to use the Google Calendar API. So far I have downloaded the latest libraries and imported them. At the moment I am trying to follow the tutorial from the Google developers which is found here.

From what I found out according to this link draft10 has been deprecated and I am trying to use other classes which do not belong to draft10.

The following are my current imports:

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;

And the following is the method taken from the Google sample with some changes:

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");
      }

The only problem is with the GoogleAccessProtectedResource class. It is giving me the following error: GoogleAccessProtectedResource cannot be resolved to a type.

Does anyone have any ideas on how I can get around this?

Was it helpful?

Solution

I managed to figure this out. All I had to do was to import the following packages:

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

And replace the following code:

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

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

With this code:

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top