Insert webProperty with Java's "Google APIs Client Library"

57 views
Skip to first unread message

Santiago Ferrer Deheza

unread,
Jan 14, 2015, 5:42:19 AM1/14/15
to google-analytic...@googlegroups.com
Hi Everyone!

First of all, sorry if my question was answer before, this is my first day using this group.

I am trying to create a webProperty using the Java client but without success. This is my code (it is actually a groovy class):

package com.creditpal.services


import com.google.api.client.auth.oauth2.Credential
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.googleapis.json.GoogleJsonResponseException
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.services.analytics.Analytics
import com.google.api.services.analytics.AnalyticsScopes
import com.google.api.services.analytics.model.Webproperty
import org.apache.commons.io.FileUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import uk.co.futureroute.lib.logging.annotation.MethodLogging


@Service
@MethodLogging
class GoogleAnalyticsServiceImpl implements GoogleAnalyticsService {


   
private static final String APPLICATION_NAME = "##THE NAME##"


   
private static final String EMAIL_ADDRESS = "##THE EMAIL ADDRESS##"


   
private static Logger logger = LoggerFactory.getLogger(GoogleAnalyticsServiceImpl.class)


   
private JsonFactory jsonFactory


   
private HttpTransport httpTransport


   
private Analytics analytics


   
public GoogleAnalyticsServiceImpl(){
        httpTransport
= GoogleNetHttpTransport.newTrustedTransport()
        jsonFactory
= JacksonFactory.getDefaultInstance()
        initializeAnalytics
()
   
}




   
@Override
   
public Webproperty insertWebProperty(String name, String websiteUrl) throws GoogleJsonResponseException {
       
Webproperty webProperty = new Webproperty(
                websiteUrl
: websiteUrl,
                name
: name
       
)


       
try {
           
return analytics.management().webproperties().insert("##ACCOUNT_ID##", webProperty).execute()
       
} catch (GoogleJsonResponseException e) {
            logger
.error("There was a google Analytics service error: "
                   
+ e.getDetails().getCode() + " : "
                   
+ e.getDetails().getMessage());
           
throw e
       
}
   
}


   
private void initializeAnalytics() throws Exception {
        analytics
= new Analytics.Builder(httpTransport, jsonFactory, authorize()).setApplicationName(
                APPLICATION_NAME
).build();
   
}


   
private Credential authorize() throws Exception {
       
final File googlePrivateKey = File.createTempFile("googlePrivateKey", ".p12")
        googlePrivateKey
.deleteOnExit()
       
FileUtils.copyInputStreamToFile(this.class.classLoader.getResourceAsStream("resources/analytics.p12"), googlePrivateKey)


       
return new GoogleCredential.Builder()
               
.setTransport(httpTransport)
               
.setServiceAccountId(EMAIL_ADDRESS)
               
.setServiceAccountPrivateKeyFromP12File(googlePrivateKey)
               
.setServiceAccountScopes(Collections.singleton(AnalyticsScopes.ANALYTICS_MANAGE_USERS))
               
.build()
   
}
}


This is a Spring service and when this object is created i dont get any exceptions. The problem is when trying to call the insertWebProperty method, there i get this:


java.lang.NullPointerException: null
 at com
.google.api.client.json.webtoken.JsonWebSignature.signUsingRsaSha256(JsonWebSignature.java:498) ~[google-http-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:378) ~[google-api-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) ~[google-oauth-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217) ~[google-oauth-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) ~[google-http-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) ~[google-api-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) ~[google-api-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) ~[google-api-client-1.19.0.jar:1.19.0]
 at com
.google.api.client.googleapis.services.AbstractGoogleClientRequest$execute.call(Unknown Source) ~[na:na]

Anyone any idea?? Thanks in advance!


Santiago Ferrer Deheza

unread,
Jan 20, 2015, 6:13:01 AM1/20/15
to google-analytic...@googlegroups.com
Any help?? I really need this!
...
Reply all
Reply to author
Forward
0 new messages