GoogleAuthorizationCodeFlow ( Java ) に問題があります。Google の「OAuth 2.0 for Web Server Applications」を使用して、Web プロジェクト用の Google カレンダー接続を構築しようとしています。したがって、Google の Java API ライブラリを使用できます。
Google の AuthorizationCallbackServlet を使用して、アクセス トークンとリフレッシュ トークンを受け取ります。
GoogleAuthorizationCodeFlow は、作成された Credential を GoogleAuthorizationCodeFlow とその JdoCredentialStore で保持します。JDO の実装として、DataNucleus を使用しています。
static PersistenceManagerFactory pmf;
static{
Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
"org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver");
properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost:3306/rssparsetest");
properties.setProperty("javax.jdo.option.ConnectionUserName","root");
properties.setProperty("javax.jdo.option.ConnectionPassword","root");
pmf = JDOHelper.getPersistenceManagerFactory(properties);
}
public static CredentialStore JDO_CREDENTIAL_STORE = new JdoCredentialStore(pmf);
public static AuthorizationCodeFlow AUTHORIZATION_CODE_FLOW = getNewAuthorizationCodeFlow();
public static AuthorizationCodeFlow getNewAuthorizationCodeFlow(){
return new GoogleAuthorizationCodeFlow.Builder(Constants.HTTP_TRANSPORT, Constants.JSON_FACTORY,
Constants.CLIENT_ID, Constants.CLIENT_SECRET,
Collections.singleton(CalendarScopes.CALENDAR)).setCredentialStore(Constants.JDO_CREDENTIAL_STORE).setAccessType("offline")
.build();
}
ステートレス Bean での使用法:
public List<CalendarListEntry> getCalendarList() {
Credential credential = null;
AuthorizationCodeFlow authCodeFlow = Constants.getNewAuthorizationCodeFlow();
try {
credential = authCodeFlow.loadCredential("USERID");
} catch (Exception e) {
//...
}
永続化された更新トークンを使用してアクセス トークンを更新する時が来ることを除いて、すべてが正常に機能しています (カレンダー エントリを一覧表示できます。資格情報は MySQL データベースに永続化されます) (GoogleAuthorizationCodeFlow が行うこと)。
Caused by: javax.jdo.JDODataStoreException: Duplicate entry 'USERID' for key 'PRIMARY'
NestedThrowables:
java.sql.BatchUpdateException: Duplicate entry 'USERID' for key 'PRIMARY'
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:421)
at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:735)
at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:755)
at com.google.api.client.extensions.jdo.auth.oauth2.JdoCredentialStore.store(JdoCredentialStore.java:47)
要約すると、私はできる
- accessToken と refreshToken を使用して Credential を受け取り、永続化する
- カレンダー エントリの一覧表示 (アクセス トークンの期限が切れていない限り)
「javax.jdo.JDODataStoreException: Duplicate entry ' USERID ' for key 'PRIMARY'」例外を取得せずに、GoogleAuthorizationCodeFlow でアクセス トークンを更新できません。
私は何を間違っていますか?
ご助力ありがとうございます!