我有同樣的問題,我花了約1小時找到這個v3的文檔中:
服務帳戶
可用於自動/離線/計劃訪問您自己帳戶的Google Analytics數據。例如,要構建您自己的Google Analytics數據的實時信息中心並與其他用戶共享。
有您需要按照配置服務帳戶與谷歌Analytics(分析)工作幾步:
- 註冊API控制檯中的項目。
- 在Google API控制檯的「API訪問」窗格下,創建一個客戶端ID,並將「應用程序類型」設置爲「服務帳戶」。
- 登錄到Google Analytics並導航到管理部分。
- 選擇您希望應用程序有權訪問的帳戶。
- 從第2步中的API控制檯創建的客戶端ID中添加電子郵件地址,作爲選定Google Analytics帳戶的用戶。
- 按照服務帳戶的說明訪問Google Analytics數據。
在這裏閱讀更多: https://developers.google.com/analytics/devguides/reporting/core/v3/gdataAuthorization
PUH,我猜的API是如此之大谷歌是有問題的記錄是一個簡單的方法=)
然後我在看了看代碼plus-serviceaccount-cmdline-sample and analytics-cmdline-sample。這是在Playframework2 Java應用程序實現的一個非常基本的版本, 打印到System.out上面的例子:
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();
}
}
希望這有助於!
你真的後幫助我。 3.0版本的文檔非常混亂和無用。它忽略了很多重要的步驟,比如如何編譯client_secrets.json並指向這樣的'what !!?'鏈接。 再次,謝謝。 – 2012-05-23 20:32:59
該頁面被谷歌刪除我面臨這個問題從過去20天你能分享代碼什麼是你用來訪問谷歌分析數據 – 2014-09-12 06:28:25