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)
1

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

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();
  }

2 Comments

As Amit said, GoogleCredential is deprecated. He's looking for an alternative using the new GoogleCredentials.
I just tested this yesterday with GoogleCredential its not showing as deprecated to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.