最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting my Google service account's email address when I try getting my calendar's "primary" c

programmeradmin2浏览0评论

Using the following code, when I display the summary of the "primary" calendar, it displays the email address of the service account. I have shared my Google account's calendars with the service account, which, according to Gemini makes them visible to the service account. But, in addition to the strange "primary" calendar summary, it also returns zero calendars when I try to list them (it should show 2 - my primary and my family).

What am I doing wrong?

    List< String > scopes = Arrays.asList( ".readonly" );

    GoogleCredential googleCredential = GoogleCredential
            .fromStream(new FileInputStream(SERVICE_ACCOUNT_KEY_PATH))
            .createScoped( scopes);

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    Calendar calendarService = new Calendar.Builder(httpTransport, JSON_FACTORY, googleCredential)
            .setApplicationName("CalendarListExample")
            .build();

    com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("primary").execute();
    System.out.println( "Summary: " + calendar.getSummary() );

    CalendarList calendarList = calendarService.calendarList().list().execute();

    if ( calendarList.getItems().size() == 0 ) {
        System.out.println( "No calendars" );
    }

Output is:

Summary:  [email protected]
No calendars

EDIT: I changed the line where I got the "primary" calendar to this (providing my personal email address as posted by someone online):

com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("[email protected]").execute();

And it returned

Summary: [email protected]

EDIT 2: I used my calendar's summary and that actually returned an error. This makes me think both my email address and service account email address is finding SOMETHING. It also contains the correct timezone for my personal email account and the UTC timezone for the service account.

calendarService.calendars().get("mycalendarsummary").execute()

Authentication failed: 404 Not Found

GET 

{

"code": 404,

"errors": [

{

"domain": "global",

"message": "Not Found",

"reason": "notFound"

}

],

"message": "Not Found"

}

Using the following code, when I display the summary of the "primary" calendar, it displays the email address of the service account. I have shared my Google account's calendars with the service account, which, according to Gemini makes them visible to the service account. But, in addition to the strange "primary" calendar summary, it also returns zero calendars when I try to list them (it should show 2 - my primary and my family).

What am I doing wrong?

    List< String > scopes = Arrays.asList( "https://www.googleapis/auth/calendar.readonly" );

    GoogleCredential googleCredential = GoogleCredential
            .fromStream(new FileInputStream(SERVICE_ACCOUNT_KEY_PATH))
            .createScoped( scopes);

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    Calendar calendarService = new Calendar.Builder(httpTransport, JSON_FACTORY, googleCredential)
            .setApplicationName("CalendarListExample")
            .build();

    com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("primary").execute();
    System.out.println( "Summary: " + calendar.getSummary() );

    CalendarList calendarList = calendarService.calendarList().list().execute();

    if ( calendarList.getItems().size() == 0 ) {
        System.out.println( "No calendars" );
    }

Output is:

Summary:  [email protected]
No calendars

EDIT: I changed the line where I got the "primary" calendar to this (providing my personal email address as posted by someone online):

com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("[email protected]").execute();

And it returned

Summary: [email protected]

EDIT 2: I used my calendar's summary and that actually returned an error. This makes me think both my email address and service account email address is finding SOMETHING. It also contains the correct timezone for my personal email account and the UTC timezone for the service account.

calendarService.calendars().get("mycalendarsummary").execute()

Authentication failed: 404 Not Found

GET https://www.googleapis/calendar/v3/calendars/mycalendarsummary

{

"code": 404,

"errors": [

{

"domain": "global",

"message": "Not Found",

"reason": "notFound"

}

],

"message": "Not Found"

}
Share Improve this question edited Mar 16 at 21:17 Todd asked Mar 15 at 21:14 ToddTodd 3,0198 gold badges39 silver badges58 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Answering my own question...

Using a service account and doing this

CalendarList calendarList = calendarService.calendarList().list().execute();

only seems to list the service account's non-existent calendar, and NOT any shared calendars. So I don't see any items in the calendarlist.

BUT, I can access my personal email account's calendar doing the following:

com.google.api.services.calendar.model.Calendar calendar = calendarService.calendars().get("[email protected]").execute();

To see the events, I can do this:

Events events = calendarService.events().list( "[email protected]").setPageToken( pageToken ).execute();

Now I just have to figure out how to view my personal email account's OTHER calendars (using the email address returns the primary calendar)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论