我正在尝试使用YouTube Java SDK通过ID获取youtube video的所有评论。
起初,我通过设置403 Request had insufficient authentication scopes
的授权范围得到了solved
,即YouTubeScopes.YOUTUBE_FORCE_SSL
。
但是当该错误消失后,空的注释列表始终返回到CommentListResponse commentsResponse
对象。
这是我程序的输出:
Initializing client...
Client initialized. Retrieving videos...
Video data: ID=LbhFO3im6vo; Title=Java. Maven просто и понятно. Зависимости (Dependencies). Скоупы (Scopes). Модули (Modules) - L6
Comments (0):
Operation finished...
这是我的Java代码:
public class VideoDataExporter {
private static final String APPLICATION_NAME = "EvgMel-YouTubeDataExporter/1.0";
private static final java.io.File DATA_STORE_DIR =
new java.io.File(System.getProperty("user.home"), ".youtube-tmp-store/youtube_sample");
private static DataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static YouTube client;
public static void main(String[] args) {
try {
System.out.println("Initializing client...");
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
Credential credential = authorize();
client = new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
System.out.println("Client initialized. Retrieving videos...");
String videoId = "LbhFO3im6vo";
try {
VideoListResponse videos = client.videos().list("snippet").setId(videoId).execute();
List<Video> items = videos.getItems();
if (items.size() > 0) {
Video v = items.get(0);
VideoSnippet s = v.getSnippet();
System.out.println(
String.format("Video data: ID=%s; Title=%s", v.getId(), s.getTitle())
);
CommentListResponse commentsResponse = client.comments().list("snippet")
.setId(videoId)
.setMaxResults(100L)
.execute();
List<Comment> comments = commentsResponse.getItems();
System.out.println(String.format("Comments (%d):", comments.size()));
for (Comment c: comments) {
CommentSnippet cs = c.getSnippet();
System.out.println(
String.format(
"ID=%s; Text=%s; AuthorName=%s; AuthorChannel=%s",
c.getId(),
cs.getTextDisplay(),
cs.getAuthorDisplayName(),
cs.getAuthorChannelUrl()
)
);
}
} else {
System.out.println("No video was found by " + videoId);
}
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Operation finished...");
}
private static Credential authorize() throws Exception {
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(VideoDataExporter.class.getResourceAsStream("/client_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/");
System.exit(1);
}
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
List.of(YouTubeScopes.YOUTUBE_FORCE_SSL)
)
.setDataStoreFactory(dataStoreFactory)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
}
我检查了配额,一切正常。但是评论仍然是空的。 预先感谢您的帮助!