使用Java的“Google API客户端库”插入webProperty

时间:2015-01-20 13:50:06

标签: java groovy google-analytics-api

我正在尝试使用Java客户端创建webProperty但没有成功(我可以访问允许创建webProperties的beta测试组)。这是我的代码(它实际上是一个groovy类):

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()
    }
}

这是一个Spring服务,当创建这个对象时,我没有任何例外。问题是当尝试调用insertWebProperty方法时,我得到了这个:

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]

任何人都有任何想法?提前谢谢!

0 个答案:

没有答案