“Login Required” 401 Unauthorized message when calling the v3 Google Calendar API using a Service Account via OAuth 2.0

后端 未结 6 633
时光取名叫无心
时光取名叫无心 2020-12-29 06:35

First, let me explain what I am trying to do, as this is a two part question.

I am building a JAX-RS service that internally authenticates with a Google account via

6条回答
  •  暖寄归人
    2020-12-29 07:26

    OK, so I have been playing around with this and have got past the 401 error, but I am now hitting a 403. However, I will detail the code I have written that at least answers the 401 problem.

    I did not change any of the service account settings or re-generate any of the keys etc, that is all the same.

    What has change is some of the code, and I upgraded to v3r20lv1.12.0-beta of the Calendar API.

    The code below, as detailed above in the OP, remains the same, i.e.:

    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    private static final JsonFactory JSON_FACTORY = new JacksonFactory();
    .
    .
    .
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId("284XXXXXXXX@developer.gserviceaccount.com")
            .setServiceAccountScopes(CalendarScopes.CALENDAR)
            .setServiceAccountPrivateKeyFromP12File(new File("D:/3cd8XXXXXXXXXXXXXXXXXXXXXXXXXXXXXd635e-privatekey.p12"))
            .build();
    

    The code to get a handle for the Calendar has changed, but this was forced by an API change:

    Calendar calendar = new Calendar(HTTP_TRANSPORT, JSON_FACTORY, credential);
    

    At this point, I can iterate through my calendar and see the events, by running the following code (this prints an event summary):

    Events events = calendar.events().list(CALENDAR_ID).execute();
    
    for (Event event : events.getItems())
    {
        System.out.println(event.getSummary());
    }
    

    But, if I try and update an event, I get a 403 Forbidden error. I will raise a separate thread for this, and will link the two threads together.

    Thanks to all for your help!

    EDIT: here is the new thread.

提交回复
热议问题