Hi I am trying to use Google Calendar client with GoogleCredentials but not able to find any docs on how to make this work. Please add a doc or let me know how to make it work.
package com.example;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import java.io.File;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import com.google.api.services.calendar.Calendar;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
public class Main {
public static void main(String[] args) throws GeneralSecurityException, IOException {
// write your code here
System.out.println("Hello");
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("target/classes/credentials.json"));
AccessToken token = credentials.getAccessToken();
Calendar client = new Calendar.Builder((HttpTransport) GoogleNetHttpTransport.newTrustedTransport(), (JsonFactory) new GsonFactory(), (HttpRequestInitializer) credentials).build();
System.out.println(client.calendarList().list().execute());
}
}
I tried using GoogleCredential but that is depreciated now so I had to switch to GoogleCredentials. The first two lines are working till I get access token. The line where I am creating calendar client results in compile time error.
Exception in thread "main" java.lang.ClassCastException: class com.google.auth.oauth2.ServiceAccountCredentials cannot be cast to class com.google.api.client.http.HttpRequestInitializer (com.google.auth.oauth2.ServiceAccountCredentials and com.google.api.client.http.HttpRequestInitializer are in unnamed module of loader 'app')
at com.example.Main.main(Main.java:29)