Google アカウントで認証しようとすると問題が発生し、Google のカレンダーからイベントを挿入/取得します
私はこのコードを使用します:
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"
}
}
リンクをブラウザにコピーすると、プロジェクトの承認を受け入れますが、エラーが表示された後
誰かが私を助けることができますか?事前にどうもありがとう