Thanks to a little help from stvar and of course the old question I linked to in my question I figured out what I needed to do. Here is the working code:
import java.io.IOException;
import java.util.Arrays;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.services.youtube.*;
import com.google.api.services.youtube.model.Video;
public class Main {
public static void main(String[] args) throws IOException
{
YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-view-count-test").build();
YouTube.Videos.List list = youtube.videos().list(Arrays.asList("statistics"));
list.setId(Arrays.asList("kffacxfA7G4"));
String apiKey = "[redacted]";
list.setKey(apiKey);
Video v = list.execute().getItems().get(0);
System.out.println("The view count is: "+v.getStatistics().getViewCount());
}
}
The Auth
class that is used in my code is taken from here, part of Google's API samples
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…