2

编辑: 我的问题的解决方案显示......好吧,永远不要不假思索地尝试使用复制和粘贴代码!

解决方案

为了能够更改我的服务,服务永远不会是最终的。删除最终启用的setCredentials(credential)并让我联系我的联系人。

正确的 ContactsService 初始化:

/**
 * Service used to communicate with contacts feed.
 */
private ContactsService service = new ContactsService(APPLICATION_NAME);

在此之后,没有错误仍然存​​在;)

原始问题

我正在尝试构建对Google GData Contacts API V3.0的 GWT-Server-Side 调用。我正在使用 GWT 2.5.1 和 jre6。为此,我想为原生应用程序实现 API 。不是作为网络服务器服务。

我确实收到了 *ACCESS_TOKEN* 和 *REFRESH_TOKEN*。

我希望能够读取/更新/删除联系人并将它们与我的数据库同步。

尽管我发现了几个问题,但没有针对原生应用程序方法的解决方案或问题。

感谢所有试图提供帮助的人!

我的问题

ContactFeed resultFeed = service.getFeed(feedUrl, ContactFeed.class);

只会导致错误:

15:12:41,513 ERROR [STDERR] java.lang.NullPointerException: No authentication header information
15:12:41,514 ERROR [STDERR]     at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)

......

我的问题

有没有其他人收到这些问题?

我可以com.google.api.client.auth.oauth2.Credential用来授权ContactsService吗?

有没有办法自己创建这个 Header 作为解决方法?

我的代码包括进口:

联系服务初始化

private final ContactsService service = new ContactsService(APPLICATION_NAME);

导入函数

@Override
public Integer importGoogleContactData() 
{
    int numberOfImportedContacts = 0;
    System.out.println( "INFO GDataServiceImpl reached importGoogleContactData()");
    try 
    {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        //authorization
        Credential credential = authorize();
        credential.refreshToken();

        System.out.println( "INFO credential = getAccessToken() " + credential.getAccessToken() );
        System.out.println( "INFO credential = getRefreshToken() " + credential.getRefreshToken() );

        service.setOAuth2Credentials(credential);
        service.useSsl();
        System.out.println( "INFO service = getServiceVersion() " + service.getServiceVersion() );

        feedUrl = new URL( "https://www.google.com/m8/feeds/contacts/default/full" );
        System.out.println("INFO url: " + feedUrl.toString() );

        ContactFeed resultFeed = service.getFeed(feedUrl, ContactFeed.class);
        // Print the results
        System.out.println(resultFeed.getTitle().getPlainText());
        for (ContactEntry entry : resultFeed.getEntries())
        {
            numberOfImportedContacts++;
            System.out.println("INFO found Contact: " + entry.getName() );
        }
    } 
    catch (IOException e) 
    {
        System.err.println(e.getMessage());
    } 
    catch (Throwable t) 
    {
        t.printStackTrace();
    }
    return numberOfImportedContacts;
}

授权()

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception 
{
    String systemEncoding = System.getProperty("file.encoding");
    FileInputStream fileInputStream = new FileInputStream("../server/default/conf/client_secret_<CLIENT_ID>.apps.googleusercontent.com.json");
    InputStreamReader inputStreamReader = new InputStreamReader( fileInputStream , systemEncoding);
    clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, inputStreamReader );

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
        .setDataStoreFactory(dataStoreFactory).build();

    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
  } 

完成进口

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.oauth2.Oauth2;
import com.google.api.services.oauth2.model.Userinfoplus;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

完整的服务器输出:

2014-03-05 15:37:53,759 INFO  [STDOUT] (http-0.0.0.0-8080-4) INFO GDataServiceImpl reached importGoogleContactData()

2014-03-05 15:37:53,798 WARNING [com.google.api.client.util.store.FileDataStoreFactory] (http-0.0.0.0-8080-4) unable to change permissions for everybody: C:\Users\evergrin\.store\oauth2
2014-03-05 15:37:53,799 WARNING [com.google.api.client.util.store.FileDataStoreFactory] (http-0.0.0.0-8080-4) unable to change permissions for owner: C:\Users\evergrin\.store\oauth2
2014-03-05 15:37:54,333 INFO  [STDOUT] (http-0.0.0.0-8080-4) INFO credential = getAccessToken() <ACCESS_TOKEN>
2014-03-05 15:37:54,334 INFO  [STDOUT] (http-0.0.0.0-8080-4) INFO credential = getRefreshToken() <REFRESH_TOKEN>
2014-03-05 15:37:54,336 INFO  [STDOUT] (http-0.0.0.0-8080-4) INFO service = getServiceVersion() GContacts-Java/3.1.0 GData-Java/1.47.1(gzip)
2014-03-05 15:37:54,336 INFO  [STDOUT] (http-0.0.0.0-8080-4) INFO url: https://www.google.com/m8/feeds/contacts/default/full
2014-03-05 15:37:54,476 ERROR [STDERR] (http-0.0.0.0-8080-4) java.lang.NullPointerException: No authentication header information
2014-03-05 15:37:54,477 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
2014-03-05 15:37:54,486 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67)
2014-03-05 15:37:54,487 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)
2014-03-05 15:37:54,487 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
2014-03-05 15:37:54,487 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
2014-03-05 15:37:54,487 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
2014-03-05 15:37:54,487 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
2014-03-05 15:37:54,488 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.Service.getFeed(Service.java:1135)
2014-03-05 15:37:54,488 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.Service.getFeed(Service.java:998)
2014-03-05 15:37:54,488 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645)
2014-03-05 15:37:54,488 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gdata.client.Service.getFeed(Service.java:1017)
2014-03-05 15:37:54,488 ERROR [STDERR] (http-0.0.0.0-8080-4)    at sung.app.dieligen.dropkick.gwt.server.gdata.GDataServiceImpl.importGoogleContactData(GDataServiceImpl.java:185)
2014-03-05 15:37:54,488 ERROR [STDERR] (http-0.0.0.0-8080-4)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2014-03-05 15:37:54,489 ERROR [STDERR] (http-0.0.0.0-8080-4)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2014-03-05 15:37:54,489 ERROR [STDERR] (http-0.0.0.0-8080-4)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2014-03-05 15:37:54,489 ERROR [STDERR] (http-0.0.0.0-8080-4)    at java.lang.reflect.Method.invoke(Method.java:606)
2014-03-05 15:37:54,489 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
2014-03-05 15:37:54,489 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
2014-03-05 15:37:54,490 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
2014-03-05 15:37:54,490 ERROR [STDERR] (http-0.0.0.0-8080-4)    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
2014-03-05 15:37:54,490 ERROR [STDERR] (http-0.0.0.0-8080-4)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
2014-03-05 15:37:54,490 ERROR [STDERR] (http-0.0.0.0-8080-4)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
2014-03-05 15:37:54,490 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
2014-03-05 15:37:54,490 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
2014-03-05 15:37:54,491 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
2014-03-05 15:37:54,491 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
2014-03-05 15:37:54,491 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
2014-03-05 15:37:54,491 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
2014-03-05 15:37:54,492 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
2014-03-05 15:37:54,492 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
2014-03-05 15:37:54,492 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
2014-03-05 15:37:54,492 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
2014-03-05 15:37:54,492 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
2014-03-05 15:37:54,493 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
2014-03-05 15:37:54,493 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
2014-03-05 15:37:54,493 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
2014-03-05 15:37:54,493 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
2014-03-05 15:37:54,493 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
2014-03-05 15:37:54,493 ERROR [STDERR] (http-0.0.0.0-8080-4)    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
2014-03-05 15:37:54,494 ERROR [STDERR] (http-0.0.0.0-8080-4)    at java.lang.Thread.run(Thread.java:744)
4

0 回答 0