ionCodeFlow与JdoCredentialStore和D

ionCodeFlow与JdoCredentialStore和D

本文介绍了重复输入使用GoogleAuthorizationCodeFlow与JdoCredentialStore和DataNucleus的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GoogleAuthorizationCodeFlow( Java )的问题。我正在使用Google的OAuth 2.0 for Web Server应用程序为我的Web项目构建Google日历连接。因此,您可以使用Google的Java api库。

I have a problem with the GoogleAuthorizationCodeFlow (Java). I am trying to build a Google Calender connection for my web project using Google's "OAuth 2.0 for Web Server Applications". Therefore you are able to use Google's Java api libraries.

我使用Google的AuthorizationCallbackServlet接收访问和刷新令牌。

I use Google's AuthorizationCallbackServlet to recieve an access and refresh token.

GoogleAuthorizationCodeFlow使用GoogleAuthorizationCodeFlow持续创建的凭证,他们的JdoCredentialStore。作为JDO实现,我使用DataNucleus。

GoogleAuthorizationCodeFlow persists the created Credential with GoogleAuthorizationCodeFlow and their JdoCredentialStore. As a JDO implementation I am using 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中的用法:

Usage in stateless Bean:

public List<CalendarListEntry> getCalendarList() {
    Credential credential = null;


    AuthorizationCodeFlow authCodeFlow = Constants.getNewAuthorizationCodeFlow();

    try {
        credential = authCodeFlow.loadCredential("USERID");
    } catch (Exception e) {
    //...
}


$ b $一切都工作正常(我可以列出我的日历条目,Credential被保存在我的MySQL数据库中)除了时间来使用持久的刷新令牌来重新取消访问令牌(GoogleAuthorizationCodeFlow )。

Everything is working fine (I am able to list my calendar entries, Credential is persisted in my MySQL database) except time comes to referesh the access token with the persisted refresh token (what GoogleAuthorizationCodeFlow does).

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)

总结起来,我可以


  1. 使用accessToken和refreshToken
  2. $接收并持有凭据b $ b
  3. 列出日历条目(只要访问令牌的时间没有过期)

  1. Receive and persist the Credential with accessToken and refreshToken
  2. List calendar entries (as long as access token's time has not expired)

我不能刷新我的a使用GoogleAuthorizationCodeFlow的ccess令牌,而不会为关键的PRIMARY异常获取javax.jdo.JDODataStoreException:Duplicate entry USERID

I am not able to refresh my access token with the GoogleAuthorizationCodeFlow without obtaining an "javax.jdo.JDODataStoreException: Duplicate entry 'USERID' for key 'PRIMARY'" exception.

我究竟做错了什么?

非常感谢您的帮助!

推荐答案

刷新你可能意味着坚持,因为那是你在做什么。而且它仍然存在与现有的相同的id。如果您打算更新它,那么您检索它并进行更新,并在数据存储区中进行更新。如果这是一些Google lib在幕后的持久性,那么请问他们的API在哪里是一个更新调用。

By "refresh" you presumably mean "persist", since that is what you're doing. And it's persisting one with the same "id" as an existent one. If you meant to update it then you retrieve it and update it and it gets updated in datastore. If this is some Google lib doing the persistence behind the scenes then ask them where in their API is an "update" call possible.

这篇关于重复输入使用GoogleAuthorizationCodeFlow与JdoCredentialStore和DataNucleus的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 05:22