2

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)
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Amit
  • 29
  • 4
  • Does this answer your question? [What is the alternative to the deprecated 'GoogleCredential'?](https://stackoverflow.com/questions/57972607/what-is-the-alternative-to-the-deprecated-googlecredential) – Guido Aug 17 '23 at 23:52

2 Answers2

2

Build the client this way, using HttpCredentialsAdapter:

GoogleCredentials credentials = ...;

Calendar service = new Calendar.Builder(
                GoogleNetHttpTransport.newTrustedTransport(),
                GsonFactory.getDefaultInstance(),
                new HttpCredentialsAdapter(credentials))
            .build();

Related questions:

Guido
  • 46,642
  • 28
  • 120
  • 174
0

I think your following an old example

private static Calendar initializeAnalyticsReporting() throws GeneralSecurityException, IOException {

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    GoogleCredential credential = GoogleCredential
        .fromStream(new FileInputStream(KEY_FILE_LOCATION))
        .createScoped(CalendarScopes.all());

    // Construct the Calendar service object.
    return new Calendar.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();
  }
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449