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.