有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java列出Google日历中的所有事件

我想使用以下代码列出Google日历中的所有事件:

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 = "edrhrtherherh@development-1384.iam.gserviceaccount.com";

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

但当我运行代码Firefox(默认web浏览器)时,我会被重定向到页面:

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.

我想把谷歌日历中的所有内容都配置到我的帐户中

我如何解决这个问题


共 (1) 个答案

  1. # 1 楼答案

    如何修复错误:重定向\u uri\u不匹配

    enter image description here 转到您的GDC并检查您指定为URI重定向的内容。应该是 http://localhost:portnumber/oauth2callback

    不要忘记“oauth2”部分