当我尝试使用我的谷歌帐户进行身份验证以从我的谷歌日历中插入/获取事件时遇到问题
我使用这个代码:
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");
}
}
我不知道为什么每次运行此代码时,我的浏览器都会向我报告此响应:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
当我将链接复制到浏览器时,我接受了我的项目的授权,但是在出现错误之后
有人可以帮助我吗?非常感谢提前