我正在尝试将Google Analytics API与Java一起使用。我下载了API客户端库的最新版本,似乎已经拥有了我需要的所有其他包。我的问题是,Google作为其Analytics API教程的一部分提供的示例代码使用了Analytics.builder()
方法(见下面的代码),但该方法似乎不在Analytics类中。有什么想法吗?提前谢谢。
/*
* Google Analytics API test.
*/
package helloanalyticsapi;
import com.google.api.client.auth.oauth2.*;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
//import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.http.HttpTransport;
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.oauth2.*;
import java.util.Arrays;
/**
*
* @author basset
*/
public class HelloAnalyticsApi {
// Global instance of the HTTP transport.
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
// Global instance of the JSON factory.
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static Analytics initializeAnalytics() throws Exception {
Credential credential = OAuth2Native.authorize(
HTTP_TRANSPORT, JSON_FACTORY, new LocalServerReceiver(),
Arrays.asList(AnalyticsScopes.ANALYTICS_READONLY));
Analytics analytics;
analytics = Analytics.builder(HTTP_TRANSPORT, JSON_FACTORY)
.setApplicationName("Hello-Analytics-API-Sample")
.setHttpRequestInitializer(credential)
.build();
return analytics;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
}
}
看起来它已经被删除,现在你必须用替换构建链
Analytics analytics = new Analytics(HTTP_TRANSPORT, JSON_FACTORY, credential)
该教程已有一年多的历史,似乎没有跟上图书馆的步伐。
我在com.google.api.services.analytics.Analytics
中有Analytics.Builder()方法,我使用的是GA v3