Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 1st, 2014  |  syntax: None  |  size: 5.80 KB  |  views: 78  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import com.google.api.client.http.HttpRequestFactory;
  2. import com.google.api.client.http.HttpResponseException;
  3. import com.google.api.client.http.HttpTransport;
  4. import com.google.api.client.http.javanet.NetHttpTransport;
  5. import com.google.api.client.json.JsonFactory;
  6. import com.google.api.client.json.jackson.JacksonFactory;
  7. import com.google.api.client.util.DateTime;
  8. //import com.google.api.services.calendar.Calendar;
  9. import com.google.api.services.calendar.model.Calendar;
  10. import com.google.api.services.calendar.model.Event;
  11. import com.google.api.services.calendar.model.EventAttendee;
  12. import com.google.api.services.calendar.model.EventDateTime;
  13. import com.google.api.client.googleapis.services.GoogleClient.Builder;
  14. //import com.google.api.services.calendar.Calendar.Calendars;
  15. //import com.google.api.services.calendar.Calendar.Calendars.Insert;
  16. import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
  17. import com.google.api.client.auth.oauth2.draft10.AccessProtectedResource.Method;
  18. import com.google.api.client.auth.oauth2.draft10.AccessTokenErrorResponse;
  19. import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
  20. import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
  21. import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant;
  22. import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
  23. import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAuthorizationRequestUrl;
  24.  
  25. import java.io.BufferedReader;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.security.GeneralSecurityException;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32.  
  33. @SuppressWarnings({ "deprecation", "unused" })
  34. public class connect{
  35.  
  36.     public void setUp() throws IOException {
  37.  
  38.         HttpTransport httpTransport = new NetHttpTransport();
  39.         JacksonFactory jsonFactory = new JacksonFactory();
  40.  
  41.         // The clientId and clientSecret are copied from the API Access tab on
  42.         // the Google APIs Console
  43.         String clientId = "MYCLIENTID";
  44.         String clientSecret = "CLIENTSECRET";
  45.  
  46.         // Or your redirect URL for web based applications.
  47.         String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
  48.         String scope = "https://www.googleapis.com/auth/calendar";
  49.  
  50.         // Step 1: Autorizzazione -->
  51.         String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)
  52.             .build();
  53.  
  54.         // Point or redirect your user to the authorizationUrl.
  55.         System.out.println("Vai al seguente indirizzo nel browser:");
  56.         System.out.println(authorizationUrl);
  57.  
  58.         // Read the authorization code from the standard input stream.
  59.         System.out.println("Qual e' il tuo codice di autorizzazione?");
  60.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  61.         String code = in.readLine();
  62.  
  63.         // Fine Step 1 <--
  64.         // Step 2: Scambio -->
  65.         AccessTokenResponse authResponse = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
  66.                 clientId, clientSecret, code, redirectUrl).execute();
  67.         System.out.println("Scrivi: "+authResponse);
  68.         System.out.println("Token d'accesso: "+authResponse.accessToken);
  69.         if(scope == "https://www.googleapis.com/auth/calendar")
  70.         System.out.println("Scope di lettura e scrittura usato :"+scope);
  71.         else
  72.             System.out.println("Scope di sola lettura usato :"+scope);
  73.         // Fine Step 2 <--
  74.  
  75.         GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
  76.                 authResponse.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
  77.                 authResponse.refreshToken);
  78.         System.out.println("Il Client ID utilizzato e': "+accessProtectedResource.getClientId());
  79.         System.out.println("Il Secret ID utilizzato e': "+accessProtectedResource.getClientSecret());
  80.         System.out.println("L'url di autenticazione e': "+accessProtectedResource.getAuthorizationServerUrl());
  81.  
  82.         com.google.api.services.calendar.Calendar service = new com.google.api.services.calendar.Calendar(httpTransport, jsonFactory);
  83.         Calendar cale = new Calendar();
  84.  
  85.         System.out.println("Menu'");
  86.         System.out.println("Cosa desideri fare?");
  87.         System.out.println("1) Creare un calendario");
  88.         System.out.println("2) Creare un evento");
  89.         System.out.println("3) Eliminare un calendario");
  90.         System.out.println("0) Uscita");
  91.  
  92.         int scelta = Integer.parseInt(in.readLine());
  93.         System.out.println(scelta);
  94.         switch(scelta){
  95.         case 1:
  96.             //insert calendar
  97.             Calendar ClaudioCal = new Calendar();
  98.             ClaudioCal.setSummary("Esempio di calendario di Claudio");
  99.             ClaudioCal.setTimeZone("America/Los_Angeles");
  100.  
  101.             cale = service.calendars().insert(ClaudioCal).execute();
  102.             break;
  103.  
  104.             //other case.......
  105.  
  106.         System.out.println("Fatto");
  107.     }
  108.     }
  109.        
  110. Credential credential = OAuth2Native.authorize(
  111.     HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(),
  112.     Arrays.asList(CalendarScopes.CALENDAR));
  113.  
  114. // set up global Calendar instance
  115. client = com.google.api.services.calendar.Calendar.builder(HTTP_TRANSPORT, JSON_FACTORY)
  116.     .setApplicationName("Google-CalendarSample/1.0").setHttpRequestInitializer(credential)
  117.     .build();
  118.        
  119. com.google.api.services.calendar.Calendar service =
  120.     new com.google.api.services.calendar.Calendar(httpTransport,
  121.                                                   jsonFactory);
  122.        
  123. com.google.api.services.calendar.Calendar service =
  124.     new com.google.api.services.calendar.Calendar(httpTransport,
  125.                                                   accessProtectedResource,
  126.                                                   jsonFactory);
clone this paste RAW Paste Data