EDIT: The solution to my problem shows... well, NEVER try to work with copy & paste code without thinking!
SOLUTION
To be able to change my Service, the Service should NEVER be FINAL. Removing final enabled setCredentials(credential) and let me reach my contacts.
Correct ContactsService init:
/**
* Service used to communicate with contacts feed.
*/
private ContactsService service = new ContactsService(APPLICATION_NAME);
after this, no errors remained ;)
Original Question
I'm trying to build a GWT-Server-Side call to Google GData Contacts API V3.0. I'm using GWT 2.5.1 and jre6. For this I want to implement the API for native application. Not as a web-server-service.
I do receive *ACCESS_TOKEN* and *REFRESH_TOKEN*.
I want to be able, to read/update/delete contacts and synchronize them with my database.
Although I found several questions, no solution or problem was directed at the native application approach.
Thanks for everybody trying to help!
My Problem
ContactFeed resultFeed = service.getFeed(feedUrl, ContactFeed.class);
only results in ERROR:
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)
.....
My question
Did anybody else receive those problems?
Can I use com.google.api.client.auth.oauth2.Credential
to authorize the ContactsService
?
Is there a way to create this Header myself as a workaround?
My Code including imports:
ContactSerice Initialization
private final ContactsService service = new ContactsService(APPLICATION_NAME);
ImportFunction
@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;
}
authorize()
/** 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");
}
Complete imports
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;
Complete Server output:
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)