Ho avuto lo stesso problema e mi ha preso circa 1h per trovare questo nella documentazione v3:
account di servizio
Utile per automatizzato/offline/accesso programmato ai dati di Google Analytics per il tuo account. Ad esempio, per creare una dashboard in tempo reale dei tuoi dati di Google Analytics e condividerla con altri utenti.
Ci sono alcuni passaggi che è necessario seguire per configurare gli account di servizio per lavorare con Google Analytics:
- registrare un progetto nella console API.
- Nella Console delle API di Google, nel riquadro Accesso API, creare un ID client con il Tipo di applicazione impostato su Account servizio.
- Accedi a Google Analytics e vai alla sezione Amministrazione.
- Selezionare l'account a cui si desidera che l'applicazione abbia accesso.
- Aggiungere l'indirizzo di posta elettronica, dall'ID cliente creato nella console API dal passaggio 2, come utente dell'account Google Analytics selezionato.
- Seguire le istruzioni per Account di servizio per accedere ai dati di Google Analytics.
Read more here: https://developers.google.com/analytics/devguides/reporting/core/v3/gdataAuthorization
Puh, immagino che l'API è così grande Google sta avendo problemi a documentare un modo semplice =)
Poi ho guardato il codice nella plus-serviceaccount-cmdline-sample e analytics-cmdline-sample. Si tratta di una versione molto di base implementata in Java app Playframework2 che stampe a System.out come esempi precedenti:
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
public static Result index() {
GoogleCredential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("[email protected]")
.setServiceAccountScopes(Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY))
.setServiceAccountPrivateKeyFromP12File(new File("/your/path/to/privatekey/privatekey.p12"))
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Set up and return Google Analytics API client.
Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(
"Google-Analytics-Hello-Analytics-API-Sample").build();
String profileId = "";
try {
profileId = getFirstProfileId(analytics);
} catch (IOException e) {
e.printStackTrace();
}
GaData gaData = null;
try {
gaData = executeDataQuery(analytics, profileId);
} catch (IOException e) {
e.printStackTrace();
}
printGaData(gaData);
return ok(index.render("Your new application is ready."));
}
private static String getFirstProfileId(Analytics analytics) throws IOException {
String profileId = null;
// Query accounts collection.
Accounts accounts = analytics.management().accounts().list().execute();
if (accounts.getItems().isEmpty()) {
System.err.println("No accounts found");
} else {
String firstAccountId = accounts.getItems().get(0).getId();
// Query webproperties collection.
Webproperties webproperties =
analytics.management().webproperties().list(firstAccountId).execute();
if (webproperties.getItems().isEmpty()) {
System.err.println("No Webproperties found");
} else {
String firstWebpropertyId = webproperties.getItems().get(0).getId();
// Query profiles collection.
Profiles profiles =
analytics.management().profiles().list(firstAccountId, firstWebpropertyId).execute();
if (profiles.getItems().isEmpty()) {
System.err.println("No profiles found");
} else {
profileId = profiles.getItems().get(0).getId();
}
}
}
return profileId;
}
/**
* Returns the top 25 organic search keywords and traffic source by visits. The Core Reporting API
* is used to retrieve this data.
*
* @param analytics the analytics service object used to access the API.
* @param profileId the profile ID from which to retrieve data.
* @return the response from the API.
* @throws IOException tf an API error occured.
*/
private static GaData executeDataQuery(Analytics analytics, String profileId) throws IOException {
return analytics.data().ga().get("ga:" + profileId, // Table Id. ga: + profile id.
"2012-01-01", // Start date.
"2012-01-14", // End date.
"ga:visits") // Metrics.
.setDimensions("ga:source,ga:keyword")
.setSort("-ga:visits,ga:source")
.setFilters("ga:medium==organic")
.setMaxResults(25)
.execute();
}
/**
* Prints the output from the Core Reporting API. The profile name is printed along with each
* column name and all the data in the rows.
*
* @param results data returned from the Core Reporting API.
*/
private static void printGaData(GaData results) {
System.out.println("printing results for profile: " + results.getProfileInfo().getProfileName());
if (results.getRows() == null || results.getRows().isEmpty()) {
System.out.println("No results Found.");
} else {
// Print column headers.
for (GaData.ColumnHeaders header : results.getColumnHeaders()) {
System.out.printf("%30s", header.getName());
}
System.out.println();
// Print actual data.
for (List<String> row : results.getRows()) {
for (String column : row) {
System.out.printf("%30s", column);
}
System.out.println();
}
System.out.println();
}
}
Spero che questo aiuti!
Il tuo post mi aiuta davvero. Il documento della versione 3.0 è così confuso e inutile. Manca un sacco di passaggi importanti come come compilare client_secrets.json e punta a tale 'cosa !!?' collegamenti. Ancora grazie. –
quella pagina rimossa da google sto affrontando questo problema negli ultimi 20 giorni puoi condividere il codice esattamente come sei abituato ad accedere ai dati analitici google –