我正在尝试将文件上传到我的 youtube 帐户,但不知何故我无法让它工作。任何帮助,将不胜感激。
这是我的代码:
public class YouTubeUploadService {
String apiKey = "real-dev-api-key";
String uName = "user_name";
String uPassword= "user_password";
public static void uploadVideo() throws Exception{
YouTubeService service2 = new YouTubeService("olala", apiKey);
try {
service2.setUserCredentials("uName", "uPassword");
} catch (AuthenticationException e) {
System.out.println("Invalid login credentials.");
System.exit(1);
}
System.out.println(service2.getVersion());
UserProfileEntry userProfileEntry = service2.getEntry(new URL(profileUrl), UserProfileEntry.class);
System.out.println(userProfileEntry.getAge());
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent("My Test Movie");
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("key word 1");
mg.getKeywords().addKeyword("key word 2");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent("plain text");
mg.setPrivate(false);
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "tag scheme1 "));
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "tag scheme 2"));
newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
newEntry.setLocation("Mountain View, CA");
MediaFileSource ms = new MediaFileSource(new File("Wildlife.wmv"), "video/quicktime");
newEntry.setMediaSource(ms);
String uploadUrl = "https://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
System.out.println("Uploading");
VideoEntry createdEntry = service2.insert(new URL(uploadUrl), newEntry);
createdEntry.update();
System.out.println("Done ---- Uploading");
}
}
当我运行此代码时,我得到:
Exception in thread "main" com.google.gdata.util.ServiceForbiddenException: Developer key required for this operation
ServiceForbiddenException
此操作需要GData Developer 密钥
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:605)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.media.MediaService.insert(MediaService.java:400)
...
当我使用代码“service2.setUserCredentials("uName", "uPassword");" 删除 try{} 捕获块时 我收到此错误:
Exception in thread "main" com.google.gdata.util.AuthenticationException: Unauthorized
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.media.MediaService.insert(MediaService.java:400)...
我在这里想念什么?我从谷歌文档中得到了这段代码。我是否需要在某处授予对我帐户的访问权限?如果是这样,请解释如何,因为我在谷歌文档上找不到它。