youtube api upload file - 此操作所需的开发人员密钥

时间:2012-05-13 21:12:42

标签: 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

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

当我删除try {} catch block时,代码为“service2.setUserCredentials(”uName“,”uPassword“);”我收到这个错误:

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

我在这里缺少什么?我从谷歌文档中获取此代码。 我是否需要在某处授予对我帐户的访问权限?如果是这样,请解释如何,因为我无法在谷歌文档中找到它。

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的,我是以错误的方式设置属性的值,String apiKey =“real-dev-api-key”。这是固定的,现在一切正常。