import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponseException;
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.jackson.JacksonFactory;
import com.google.api.client.util.DateTime;
//import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventAttendee;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.api.client.googleapis.services.GoogleClient.Builder;
//import com.google.api.services.calendar.Calendar.Calendars;
//import com.google.api.services.calendar.Calendar.Calendars.Insert;
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.draft10.AccessProtectedResource.Method;
import com.google.api.client.auth.oauth2.draft10.AccessTokenErrorResponse;
import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
@SuppressWarnings({ "deprecation", "unused" })
public class connect{
public void setUp() throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// The clientId and clientSecret are copied from the API Access tab on
// the Google APIs Console
String clientId = "MYCLIENTID";
String clientSecret = "CLIENTSECRET";
// Or your redirect URL for web based applications.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
String scope = "https://www.googleapis.com/auth/calendar";
// Step 1: Autorizzazione -->
String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)
.build();
// Point or redirect your user to the authorizationUrl.
System.out.println("Vai al seguente indirizzo nel browser:");
System.out.println(authorizationUrl);
// Read the authorization code from the standard input stream.
System.out.println("Qual e' il tuo codice di autorizzazione?");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String code = in.readLine();
// Fine Step 1 <--
// Step 2: Scambio -->
AccessTokenResponse authResponse = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
clientId, clientSecret, code, redirectUrl).execute();
System.out.println("Scrivi: "+authResponse);
System.out.println("Token d'accesso: "+authResponse.accessToken);
if(scope == "https://www.googleapis.com/auth/calendar")
System.out.println("Scope di lettura e scrittura usato :"+scope);
else
System.out.println("Scope di sola lettura usato :"+scope);
// Fine Step 2 <--
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
authResponse.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
authResponse.refreshToken);
System.out.println("Il Client ID utilizzato e': "+accessProtectedResource.getClientId());
System.out.println("Il Secret ID utilizzato e': "+accessProtectedResource.getClientSecret());
System.out.println("L'url di autenticazione e': "+accessProtectedResource.getAuthorizationServerUrl());
com.google.api.services.calendar.Calendar service = new com.google.api.services.calendar.Calendar(httpTransport, jsonFactory);
Calendar cale = new Calendar();
System.out.println("Menu'");
System.out.println("Cosa desideri fare?");
System.out.println("1) Creare un calendario");
System.out.println("2) Creare un evento");
System.out.println("3) Eliminare un calendario");
System.out.println("0) Uscita");
int scelta = Integer.parseInt(in.readLine());
System.out.println(scelta);
switch(scelta){
case 1:
//insert calendar
Calendar ClaudioCal = new Calendar();
ClaudioCal.setSummary("Esempio di calendario di Claudio");
ClaudioCal.setTimeZone("America/Los_Angeles");
cale = service.calendars().insert(ClaudioCal).execute();
break;
//other case.......
System.out.println("Fatto");
}
}
Hi people, this is my code to connect at Google Account to modify the Calendar, but everytime the system response me “Unauthorized”. Like as the accessToken isn’t verified, i don’t understand where’s the problem
please help me
thanks a lot in advance
In the latest version of the Java API library, the mechanism for handling OAuth has changed significantly.
You’ll want to follow the builder pattern referenced in the sample code here:
http://code.google.com/p/google-api-java-client/source/browse/calendar-cmdline-sample/src/main/java/com/google/api/services/samples/calendar/cmdline/CalendarSample.java?repo=samples#57
This automatically handles the prompting for you (and even does some great magic on desktops to open up the browser). Alternatively, if you’re looking for how to do this under the covers, more information is here:
http://code.google.com/p/google-api-java-client/source/browse/shared/shared-sample-cmdline/src/main/java/com/google/api/services/samples/shared/cmdline/oauth2/OAuth2Native.java?repo=samples#102