我正在尝试获取Google Calendar API的授权请求。我似乎得到了一个凭证,一切都很好,但是当我尝试使用它时,我被告知我正在尝试坚持已经存在的对象。
这是授权servlet的代码,我授权用户并执行请求(回调被调用一次,后续请求不需要它)。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
somethingTest();
response.getOutputStream().println("<html><head><title>Hello</title></head><body><p>Hello world!</p></body></html>");
}
void somethingTest(){
Event event = new Event();
event.setSummary("appointment");
event.setLocation("HQ");
ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail(OTHER_ATTENDEE));
event.setAttendees(attendees);
DateTime start = DateTime.parseRfc3339("2012-06-05T10:00:00.000+01:00");
DateTime end = DateTime.parseRfc3339("2012-06-05T11:00:00.000+01:00");
event.setStart(new EventDateTime().setDateTime(start).setTimeZone("CEST"));
event.setEnd(new EventDateTime().setDateTime(end).setTimeZone("CEST"));
event.setRecurrence(Arrays.asList("RRULE:FREQ=WEEKLY;UNTIL=20120801T100000+01:00"));
Calendar calendar = Calendar.builder(HTTP_TRANSPORT, JSON_FACTORY)
.setApplicationName("CalendarSyncTest/1.0")
.setHttpRequestInitializer(getCredential()).build();
try {
// NEXT LINE THROWS EXCEPTION
Event createdEvent = calendar.events().insert("primary", event).execute();
log.trace("Event created: " + createdEvent.getId());
} catch (IOException e) {
log.warn("Exception caught mocking about", e);
}
}
@Override
protected String getRedirectUri(HttpServletRequest req)
throws ServletException, IOException {
log.trace("getRedirectUri()");
GenericUrl url = new GenericUrl(req.getRequestURL().toString());
url.setRawPath("/google-sync/Callback");
return url.build();
}
@Override
protected AuthorizationCodeFlow initializeFlow() throws ServletException,
IOException {
log.trace("InitializeFlow()");
javax.jdo.PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("transactions-optional");
return new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
CLIENT_ID, CLIENT_SECRET,
Collections.singleton(CalendarScopes.CALENDAR)).setCredentialStore(new JdoCredentialStore(pmf))
.build();
}
@Override
protected String getUserId(HttpServletRequest req) throws ServletException,
IOException {
return USER_EMAIL;
}
当然是例外:
org.datanucleus.exceptions.NucleusUserException: Object "com.google.api.client.extensions.jdo.auth.oauth2.JdoPersistedCredential@1abf87" being inserted has id [USER_EMAIL HERE] yet an object with this id already exists in the datastore!
org.datanucleus.store.xml.XMLPersistenceHandler.insertObject(XMLPersistenceHandler.java:96)
org.datanucleus.state.JDOStateManager.internalMakePersistent(JDOStateManager.java:2376)
org.datanucleus.state.JDOStateManager.makePersistent(JDOStateManager.java:2352)
org.datanucleus.ObjectManagerImpl.persistObjectInternal(ObjectManagerImpl.java:1840)
org.datanucleus.ObjectManagerImpl.persistObjectWork(ObjectManagerImpl.java:1689)
org.datanucleus.ObjectManagerImpl.persistObject(ObjectManagerImpl.java:1554)
org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:740)
org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:765)
com.google.api.client.extensions.jdo.auth.oauth2.JdoCredentialStore.store(JdoCredentialStore.java:47)
com.google.api.client.auth.oauth2.CredentialStoreRefreshListener.makePersistent(CredentialStoreRefreshListener.java:62)
com.google.api.client.auth.oauth2.CredentialStoreRefreshListener.onTokenResponse(CredentialStoreRefreshListener.java:48)
com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:497)
com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:255)
com.google.api.client.http.HttpRequest.execute(HttpRequest.java:746)
com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:182)
com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:170)
com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:163)
com.google.api.services.calendar.Calendar$Events$Insert.execute(Calendar.java:1810)
com.zylinc.core.googleSync.Test.somethingTest(Test.java:78)
com.zylinc.core.googleSync.Test.doGet(Test.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
com.google.api.client.extensions.servlet.auth.oauth2.AbstractAuthorizationCodeServlet.service(AbstractAuthorizationCodeServlet.java:129)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
我发现这一切都非常令人困惑,特别是因为Google文档是一堆乱七八糟的方法,彼此之间并不完全匹配。