2

我正在用 Java 编写代码,将视频从 mu 应用程序上传到 youtube。

我的代码是:

package com.youtube.video;

import java.io.IOException;
import java.net.URL;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.FormUploadToken;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class YouTubeController {

    public static YouTubeService service;

    public void init() {
      if (service == null) {
        service = new YouTubeService("mail@gmail.com", "A...A");
        String username = "mail@gmail.com";
        String password = "xxxxxxxxx";

        try {
          service.setUserCredentials(username, password);
        } catch (AuthenticationException ae) {
          ae.printStackTrace();
        }
      }
    }

    static String token;
    static String formUrl;
    public static void setFormDetails() throws IOException {
      VideoEntry newEntry = new VideoEntry();
      YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();

      String videoTitle = "this is a title";

      mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
      mg.setTitle(new MediaTitle());
      mg.setPrivate(false);
      mg.setKeywords(new MediaKeywords());
      mg.getKeywords().addKeyword("");
      mg.getTitle().setPlainTextContent(videoTitle);
      mg.setDescription(new MediaDescription());
      mg.getDescription().setPlainTextContent(videoTitle);

      URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");

      try {
        FormUploadToken fut = service.getFormUploadToken(uploadUrl, newEntry);
        token = fut.getToken();
        System.out.println(">>>>>"+token);
        formUrl = fut.getUrl();
      } catch (ServiceException se) {
        se.printStackTrace();
      }
    }

    public static void main(String s[]) throws IOException {
        YouTubeController yc = new YouTubeController();
        yc.init();
        yc.setFormDetails();
    }
}

我在 lib 文件夹中有以下罐子:

gdata-client-1.0.jar gdata-youtube-2.0.jar gdata-core-1.0.jar gdata-media-1.0.jar google-collect-1.0.jar guava-13.0.1.jar mail.jar activation.jar

但是在运行这段代码时,它给了我以下错误

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf([Ljava/lang/Object;)Lcom/google/common/collect/ImmutableSet;
    at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableTypes(AltFormat.java:399)
    at com.google.gdata.wireformats.AltFormat$Builder.setAcceptableXmlTypes(AltFormat.java:387)
    at com.google.gdata.wireformats.AltFormat.<clinit>(AltFormat.java:49)
    at com.google.gdata.client.Service.<clinit>(Service.java:558)
    at com.zeta.video.YouTubeController.init(YouTubeController.java:23)
    at com.zeta.video.YouTubeController.main(YouTubeController.java:66)

我在网上尝试了所有解决方案,他们都指出这个错误是因为我缺少一些 JAR 文件。但我拥有所有的 JAR。有人可以帮我解决这个问题吗?

4

1 回答 1

0

看起来好像您同时引用了google-collectionsguava。由于guavagoogle-collections的超集,您实际上只需要引用guava。删除对google-collections的引用,您应该会发现问题已解决。

于 2012-11-26T11:19:20.053 回答