java - youtube api 上传文件 - 此操作需要开发人员 key

标签 java youtube-api

我正在尝试将文件上传到我的 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

GDataServiceForbiddenException此操作需要开发人员 key

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{} catch block 时我收到此错误:

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)...

我在这里缺少什么?我从谷歌文档中得到了这段代码。 我需要在某处授予对我帐户的访问权限吗?如果是这样,请解释一下如何,因为我在谷歌文档上找不到它。

最佳答案

正如我在评论中提到的,我以错误的方式设置了属性 String apiKey = "real-dev-api-key"的值。此问题已修复,现在一切正常。

关于java - youtube api 上传文件 - 此操作需要开发人员 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575288/

相关文章:

java - java中如何找出sql结果集中的变量类型?

java - Maven exec 插件将参数传递给外部可执行文件

java - Java 局域网游戏

java - 斜面 java (带角的三角形)

java - setCellRenderer() 不起作用

javascript - JavaScript 中的 Promise( Angular )

youtube - 视频的YouTube流量来源

javascript - 有没有办法在直播时从 YouTube API 获取通知

youtube-api - YouTube IFrame API onError 值为 0 是什么意思?

android - 如何获得最多的YouTube视频信息(例如缓冲时间,视频质量等)的最佳方法是什么?