问题
Ok, so I'm trying to build a simple "Hello World"-type program that gets data from the Google Analytics API. I'm following the tutorial here:
https://developers.google.com/analytics/solutions/articles/hello-analytics-api
My code is below. There's no main method yet, I'm just trying to get authorized and get a Analytics Service Object set up. I'm working in Netbeans. I've downloaded an imported the classes from Google listed at the top of the code. My problem is that Netbeans gives me a "cannot find symbol" on the OAuth2Native.authorize()
in the first line of initializeAnalytics and on Analytics.builder()
a few lines below that. I assume there's some class I need to import, but I can't seem to find it and wonder if that's not the problem at all.
Many thanks in advance.
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 java.util.Arrays;
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;
}
public static void main(String[] args) {
}
}
来源:https://stackoverflow.com/questions/17261806/why-does-java-not-allow-me-to-use-oauth2native-methods-here