Google analytics 谷歌';针对API getID()错误的Hello Analytics程序
我在让Hello Analytics程序与Google Analytics API v3配合使用时遇到问题。我已经访问了google开发者网站,并完成了设置用户、获取凭据以及将用户添加到分析中的步骤。我从网页上下载了代码 我将jar文件添加到类路径中,并且在java代码中没有一个错误。但是,在第71、80和90行中有一个错误,表示“类型对象的方法getId()未定义” 我知道这个方法没有在类型对象中定义。我查看了调用的层次结构,发现它不存在于Google提供的.jar文件中的对象、列表或帐户级别 我正在java控制面板上运行java版本1.8.073。你能告诉我出了什么问题或者如何解决这个问题吗 以下是来自谷歌的代码:Google analytics 谷歌';针对API getID()错误的Hello Analytics程序,google-analytics,google-analytics-api,Google Analytics,Google Analytics Api,我在让Hello Analytics程序与Google Analytics API v3配合使用时遇到问题。我已经访问了google开发者网站,并完成了设置用户、获取凭据以及将用户添加到分析中的步骤。我从网页上下载了代码 我将jar文件添加到类路径中,并且在java代码中没有一个错误。但是,在第71、80和90行中有一个错误,表示“类型对象的方法getId()未定义” 我知道这个方法没有在类型对象中定义。我查看了调用的层次结构,发现它不存在于Google提供的.jar文件中的对象、列表或帐户级别
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.Accounts;
import com.google.api.services.analytics.model.GaData;
import com.google.api.services.analytics.model.Profiles;
import com.google.api.services.analytics.model.Webproperties;
import java.io.File;
import java.io.IOException;
/**
* A simple example of how to access the Google Analytics API using a service
* account.
*/
public class HelloAnalytics {
private static final String APPLICATION_NAME = "Hello Analytics";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String KEY_FILE_LOCATION = "/path/to/your.p12";
private static final String SERVICE_ACCOUNT_EMAIL = "<SERVICE_ACCOUNT_EMAIL>@developer.gserviceaccount.com";
public static void main(String[] args) {
try {
Analytics analytics = initializeAnalytics();
String profile = getFirstProfileId(analytics);
System.out.println("First Profile Id: "+ profile);
printResults(getResults(analytics, profile));
} catch (Exception e) {
e.printStackTrace();
}
}
private static Analytics initializeAnalytics() throws Exception {
// Initializes an authorized analytics service object.
// Construct a GoogleCredential object with the service account email
// and p12 file downloaded from the developer console.
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
.setServiceAccountScopes(AnalyticsScopes.all())
.build();
// Construct the Analytics service object.
return new Analytics.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
private static String getFirstProfileId(Analytics analytics) throws IOException {
// Get the first view (profile) ID for the authorized user.
String profileId = null;
// Query for the list of all accounts associated with the service account.
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 for the list of properties associated with the first account.
Webproperties properties = analytics.management().webproperties()
.list(firstAccountId).execute();
if (properties.getItems().isEmpty()) {
System.err.println("No Webproperties found");
} else {
String firstWebpropertyId = properties.getItems().get(0).getId();
// Query for the list views (profiles) associated with the property.
Profiles profiles = analytics.management().profiles()
.list(firstAccountId, firstWebpropertyId).execute();
if (profiles.getItems().isEmpty()) {
System.err.println("No views (profiles) found");
} else {
// Return the first (view) profile associated with the property.
profileId = profiles.getItems().get(0).getId();
}
}
}
return profileId;
}
private static GaData getResults(Analytics analytics, String profileId) throws IOException {
// Query the Core Reporting API for the number of sessions
// in the past seven days.
return analytics.data().ga()
.get("ga:" + profileId, "7daysAgo", "today", "ga:sessions")
.execute();
}
private static void printResults(GaData results) {
// Parse the response from the Core Reporting API for
// the profile name and number of sessions.
if (results != null && !results.getRows().isEmpty()) {
System.out.println("View (Profile) Name: "
+ results.getProfileInfo().getProfileName());
System.out.println("Total Sessions: " + results.getRows().get(0).get(0));
} else {
System.out.println("No results found");
}
}
}
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
导入com.google.api.client.http.HttpTransport;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.json.gson.GsonFactory;
导入com.google.api.services.analytics.analytics;
导入com.google.api.services.analytics.AnalyticsScopes;
导入com.google.api.services.analytics.model.Accounts;
导入com.google.api.services.analytics.model.GaData;
导入com.google.api.services.analytics.model.Profiles;
导入com.google.api.services.analytics.model.Webproperties;
导入java.io.File;
导入java.io.IOException;
/**
*如何使用服务访问Google Analytics API的简单示例
*帐户。
*/
公共类HelloAnalytics{
私有静态最终字符串应用程序\u NAME=“Hello Analytics”;
私有静态最终JsonFactory JSON_FACTORY=GsonFactory.getDefaultInstance();
私有静态最终字符串密钥文件位置=“/path/to/your.p12”;
私有静态最终字符串服务\u ACCOUNT\u EMAIL=“@developer.gserviceaccount.com”;
公共静态void main(字符串[]args){
试一试{
分析=初始化分析();
字符串配置文件=getFirstProfileId(分析);
System.out.println(“第一个配置文件Id:+Profile”);
打印结果(getResults(分析、配置文件));
}捕获(例外e){
e、 printStackTrace();
}
}
私有静态分析initializeAnalytics()引发异常{
//初始化授权的分析服务对象。
//使用服务帐户电子邮件构造GoogleCredential对象
//以及从开发者控制台下载的p12文件。
HttpTransport HttpTransport=GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential=新建GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_工厂)
.setServiceAccountId(服务\帐户\电子邮件)
.SetServiceAccountPrivateKeyFromp12文件(新文件(密钥文件位置))
.setServiceAccountScopes(AnalyticsScopes.all())
.build();
//构造分析服务对象。
返回新的Analytics.Builder(httpTransport、JSON_工厂、凭证)
.setApplicationName(应用程序名称).build();
}
私有静态字符串getFirstProfileId(分析)引发IOException{
//获取授权用户的第一个视图(配置文件)ID。
字符串profileId=null;
//查询与服务帐户关联的所有帐户的列表。
Accounts Accounts=analytics.management().Accounts().list().execute();
if(accounts.getItems().isEmpty()){
System.err.println(“未找到帐户”);
}否则{
字符串firstAccountId=accounts.getItems().get(0.getId();
//查询与第一个帐户关联的属性列表。
Webproperties属性=analytics.management().Webproperties()
.list(firstAccountId).execute();
if(properties.getItems().isEmpty()){
System.err.println(“未找到Webproperties”);
}否则{
字符串firstWebpropertyId=properties.getItems().get(0.getId();
//查询与属性关联的列表视图(配置文件)。
Profiles Profiles=analytics.management().Profiles()
.list(firstAccountId,firstWebpropertyId).execute();
if(profiles.getItems().isEmpty()){
System.err.println(“未找到视图(配置文件”);
}否则{
//返回与属性关联的第一个(视图)配置文件。
profileId=profiles.getItems().get(0.getId();
}
}
}
返回profileId;
}
私有静态GaData getResults(Analytics Analytics,String profileId)引发IOException{
//查询Core Reporting API以获取会话数
//在过去的七天里。
return analytics.data().ga()
.get(“ga:+profileId”、“7daysAgo”、“今天”、“ga:sessions”)
.execute();
}
私有静态void打印结果(GaData结果){
//解析来自的核心报告API的响应
//配置文件名称和会话数。
if(results!=null&&!results.getRows().isEmpty()){
System.out.println(“视图(配置文件)名称:”
+results.getProfileInfo().getProfileName());
System.out.println(“总会话:+results.getRows().get(0.get(0));
}否则{
System.out.println(“未找到结果”);
}
}
}
您为可变服务账户电子邮件提供的价值,即“@developer.gserviceaccount.com”,您需要将其添加到您的谷歌分析“用户管理”中。您将在管理>用户管理中找到它。在那里添加您的服务帐户电子邮件,它将开始工作。您为可变服务帐户电子邮件提供的价值,即“@developer.gserviceaccount.com”,您需要将其添加到您的谷歌分析“用户管理”中。您将在管理>用户管理中找到它。在那里添加您的服务\帐户\电子邮件,它将