2

This is my problem. I spent two days testing and searching in google about this error, but i can't understand the way to fix this problem. When i make a request to google, google says:

"error" : "invalid_grant"

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
}   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

I import this jars:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.GaData;
import com.google.api.client.http.HttpTransport;

This is my code

public static void analyticsExample()
{
// This is the .p12 file you got from the google api console by clicking generate new key
File analyticsKeyFile = new File("C:\\Analytics\\uvcanalytics-f44b89840853.p12");
// This is the service account email address that you can find in the api console
String apiEmail = "<794607131777-gl7vhg10l8vr8d7uln70udfl92r61clh@developer.gserviceaccount.com>";
GoogleCredential credential = null;
try {
    credential = new GoogleCredential.Builder()
    .setTransport(HTTP_TRANSPORT)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(apiEmail)
    .setServiceAccountScopes(Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY))
    .setServiceAccountPrivateKeyFromP12File(analyticsKeyFile).build();
} catch (Exception e) {
    System.out.print("\n Error A : " + e);
}


Analytics analyticsService = null;
try {
    analyticsService = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
    .setApplicationName("uvcanalytics")
    .build();
} catch (Exception e){System.out.print("\n ######### Bad Analytics service " + e );}


String startDate = "2015-02-15";
String endDate = "2015-02-15";
String mertrics = "ga:sessions";
String idProp = "ga:97649034";

// Use the analytics object build a query
Analytics.Data.Ga.Get get = null;
try {
    get = analyticsService.data().ga().get(idProp,startDate,endDate,mertrics);
    get.setDimensions("ga:date");
} catch (Exception e) {
    System.out.print("\n Error B : " + e);
}


// Run the query
GaData data = null;
try {
    data = get.execute();
} catch (Exception e) {
    System.out.print("\n Error C : " + e);
}

// Do something with the data
if (data.getRows() != null) {
    for (List<String> row : data.getRows()) {
        System.out.println(row);
    }
}}

Any idea how to fix this? Thanks.

Bono
  • 4,757
  • 6
  • 48
  • 77
Aneurysmo
  • 303
  • 1
  • 3
  • 9
  • Did searching about it give you any answers? You say you don't understand the solution. Does that mean you found a solution but don't know how to apply it? – Bono Feb 20 '15 at 18:12

1 Answers1

1

Finally i found a solution, think my main problem as many people is that all Google documents are so big for read quickly, but i found this article:

Google Analytics authorization in java Starts with [I had the same problem and I took me about 1h to find this in the v3 documentation]

Read carefully the answer, i just Add a comment, when you are Grant Access(my real probles was this) to propieties you must do it to your Account section and to Property section your Google developer account (xxxxxx-xr61clh@developer.gserviceaccount.com), i hope this help.

Community
  • 1
  • 1
Aneurysmo
  • 303
  • 1
  • 3
  • 9