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

GData ServiceForbiddenException此操作所需的开发人员密钥

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 块时我收到此错误:

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

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

正如我在评论中提到的,我以错误的方式设置了专有的值,字符串 apiKey = "real-dev-api-key"。这是固定的,现在一切正常。

相关内容

  • 没有找到相关文章

最新更新