Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Google Calendar Java App

I'm following along with the step-by-step configuration instructions by Google, but for some reason I can't find a few packages they're requiring me to import. The packages which my app is unable to find are (or the lines my IDE is complaining about):

import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;

I can't seem to find the jars which hold these. My classpath contains:

//The Google Calendar Client API:
google-api-services-calendar-v3-rev16-1.8.0-beta.jar

//And of course the Google API Core:
commons-logging-1.1.1.jar
google-api-client-1.11.0-beta.jar
google-api-client-1.11.0-beta.jar.properties
google-api-client-android-1.11.0-beta.jar
google-api-client-android-1.11.0-beta.jar.properties
google-api-client-android2-1.11.0-beta.jar
google-api-client-android2-1.11.0-beta.jar.properties
google-api-client-appengine-1.11.0-beta.jar
google-api-client-java6-1.11.0-beta.jar
google-http-client-1.11.0-beta.jar
google-http-client-1.11.0-beta.jar.properties
google-http-client-android-1.11.0-beta.jar
google-http-client-android-1.11.0-beta.jar.properties
google-http-client-android2-1.11.0-beta.jar
google-http-client-android2-1.11.0-beta.jar.properties
google-http-client-android3-1.11.0-beta.jar
google-http-client-android3-1.11.0-beta.jar.properties
google-http-client-appengine-1.11.0-beta.jar
google-http-client-gson-1.11.0-beta.jar
google-http-client-gson-1.11.0-beta.jar.properties
google-http-client-jackson-1.11.0-beta.jar
google-http-client-jackson-1.11.0-beta.jar.properties
google-http-client-jackson2-1.11.0-beta.jar
google-http-client-jackson2-1.11.0-beta.jar.properties
google-oauth-client-1.11.0-beta.jar
google-oauth-client-1.11.0-beta.jar.properties
google-oauth-client-appengine-1.11.0-beta.jar
google-oauth-client-java6-1.11.0-beta.jar
google-oauth-client-jetty-1.11.0-beta.jar
google-oauth-client-servlet-1.11.0-beta.jar
gson-2.1.jar
gson-2.1.jar.properties
guava-11.0.1.jar
guava-11.0.1.jar.properties
httpclient-4.0.3.jar
httpcore-4.0.1.jar
jackson-core-2.0.5.jar
jackson-core-2.0.5.jar.properties
jackson-core-asl-1.9.9.jar
jackson-core-asl-1.9.9.jar.properties
jdo2-api-2.3-eb.jar
jetty-6.1.26.jar
jetty-util-6.1.26.jar
jsr305-1.3.9.jar
transaction-api-1.1.jar
xpp3-1.1.4c.jar

I'm not sure what it is I'm missing, but I need those libraries to continue the tutorial. If more information is necessary I'll be happy to provide it. I'm a newbie when it comes to the Google Calendar API. Any help is appreciated! Thanks!

like image 853
kentcdodds Avatar asked Dec 15 '22 19:12

kentcdodds


1 Answers

Unfortunately, as of now, Google has not updated the Java configuration source code. You do not need those classes, and as others in their comments pointed out, they are deprecated.

Substitute the "draft10" imports by:

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.services.calendar.CalendarScopes;

Then, substitute the authorization code (from comment "Step 1: Authorize -->" onwards ) by:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, clientId, clientSecret,
            Arrays.asList(CalendarScopes.CALENDAR)).setAccessType("online")
                .setApprovalPrompt("auto").build();

String url = flow.newAuthorizationUrl().setRedirectUri(redirectUrl).build();
System.out.println("Please open the following URL in your browser then type the authorization code:");

System.out.println("  " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();

GoogleTokenResponse response = flow.newTokenRequest(code)
                .setRedirectUri(redirectUrl).execute();
GoogleCredential credential = new GoogleCredential()
                .setFromTokenResponse(response);

// Create a new authorized API client
Calendar service = new Calendar.Builder(httpTransport, jsonFactory,
                credential).build();

I had the same problem, and found the drive sample code to be up to date. I guessed my way through, and made it work. "Authorization Code Flow" is described here.

like image 129
Felix Dobslaw Avatar answered Jan 03 '23 05:01

Felix Dobslaw