I have a problem when i try to authenticate with my google account, to 
insert/get events from my calendar on google

i use this code:
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Calendars;
import com.google.api.services.calendar.Calendar.Calendars.Insert;
import 
com.google.api.client.auth.oauth2.draft10.AccessProtectedResource.Method;
import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
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.IOException;
import java.io.InputStreamReader;


@SuppressWarnings({ "deprecation", "unused" })
public class connect{

//connessione Google for unauthorized user for a limited usage
/*JsonHttpRequestInitializer initializer = new 
GoogleKeyInitializer("AIzaSyCuWqLpR9G4QhfJ1QMbqh3pa0vul6sd8yQ");
    Plus plus = Plus.builder(new NetHttpTransport(), new JacksonFactory(), 
new GenericUrl())
        .setApplicationName("Progetto SITI")
        .setJsonHttpRequestInitializer(initializer)
        .build();*/
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 = " MYSECRETID ";

    // Or your redirect URL for web based applications.
    String redirectUrl = 
"https://www.googleapis.com/calendar/v3/calendars/{calendarID}";;
    String scope = "https://www.googleapis.com/auth/calendar";;

    // Step 1: Authorization-->
    String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, 
redirectUrl, scope)
        .build();

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Navigate the link on your browser:");
    System.out.println(authorizationUrl);

    // Read the authorization code from the standard input stream.
    System.out.println("What's your authorization code?");
    BufferedReader in = new BufferedReader(new 
InputStreamReader(System.in));
    String code = in.readLine();
    // EndStep 1 <--

    // Step 2: Change-->
    AccessTokenResponse authResponse = new 
GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
            clientId, clientSecret, code, redirectUrl).execute();
    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);
    // End Step 2 <--

GoogleAccessProtectedResource accessProtectedResource = new 
GoogleAccessProtectedResource(
authResponse.accessToken, httpTransport, jsonFactory, clientId, 
clientSecret,
authResponse.refreshToken);
System.out.println("Client ID: "+accessProtectedResource.getClientId());
System.out.println("Secret ID: "+accessProtectedResource.getClientSecret());
System.out.println("Authentication Url: 
"+accessProtectedResource.getAuthorizationServerUrl());

Calendar prova = new Calendar(httpTransport, jsonFactory);
Calendar cale = new Calendar(httpTransport, jsonFactory);
com.google.api.services.calendar.model.Calendar cal = new 
com.google.api.services.calendar.model.Calendar();
cal.setSummary("My test Calendar");
    cal.setDescription("This calendar was created with the API v3.");
    cal.setTimeZone("Europe/Amsterdam");
    cal.setLocation("Amsterdam");
    cal.setId("4");
    String summary = cal.getSummary();
    System.out.println(summary);
    prova.calendars().insert(cal);
    prova.calendars().delete("Caldario prova");
//Calendar calendarService = null;
//Calendars calendarList = cale.calendars();
    //Insert insert = calendarList.insert(cal);
    //cal = insert.execute();
    System.out.println("Job Done");
  }
}

i don't know why every time i run this code my browser report me this 
response:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}


when i copy the link to the browser i accept the authorization for my project, 
but after the error appaer


someone can help me?

thanks a lot in advance

-- 
You received this message because you are subscribed to the Google Groups 
"iGoogle Developer Forum" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/Google-Gadgets-API/-/zGq8B8MTG4gJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/Google-Gadgets-API?hl=en.

Reply via email to