Peter Penzov
Peter Penzov

Reputation: 1708

List all events from Google calendar

I want to list all events all events from Google calendar using this code:

public class GoogleCalendarImpl
{
    private static final String APPLICATION_NAME = "";
    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".store/calendar_sample");
    private static FileDataStoreFactory dataStoreFactory;
    private static HttpTransport httpTransport;
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    private static com.google.api.services.calendar.Calendar client;

    static final java.util.List<Calendar> addedCalendarsUsingBatch = Lists.newArrayList();

    private static final String calId = "[email protected]";

    private static Credential authorize() throws Exception
    {
        // load client secrets
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(GoogleCalendarImpl.class.getResourceAsStream("/development-241a19899242.json")));

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, clientSecrets,
            Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory).build();
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }

    public static void main(String[] args)
    {
        try
        {
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
            Credential credential = authorize();
            client = new com.google.api.services.calendar.Calendar.Builder(
                httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();

            getAllEvents();

        }
        catch (IOException e)
        {
            System.err.println(e.getMessage());
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
        System.exit(1);
    }

    private static List<Event> getAllEvents() throws IOException
    {
        List<Event> events = new ArrayList<>();
        String nextPageToken = null;
        do
        {
            System.out.println("Loading page " + nextPageToken);
            Events feed = client.events().list(calId).setPageToken(nextPageToken).execute();
            events.addAll(feed.getItems());
            nextPageToken = feed.getNextPageToken();
        }
        while (nextPageToken != null);

        return events;
    }
}

But when I run the code Firefox(default web browser) is started and I'm redirected to page:

Error: redirect_uri_mismatch

The redirect URI in the request, http://localhost:56345/Callback, does not match the ones authorized for the OAuth client. Visit https://console.developers.google.com/apis/credentials/oauthclient/1024206104045435454813?project=762076316631 to update the authorized redirect URIs.

I would like to get all entried from Google calendar configured into my account.

How I can fix this issue?

Upvotes: 0

Views: 705

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

How to fix Error: redirect_uri_mismatch

enter image description here Go to your GDC and check what you specified as URI redirect. It should be http://localhost:portnumber/oauth2callback.

Don't forget the 'oauth2' part.

Upvotes: 1

Related Questions