2012-05-13 49 views
1

我想將文件上傳到我的YouTube帳戶,但不知何故,我無法讓它正常工作。 任何幫助,將不勝感激。youtube api上傳文件 - 此操作所需的開發人員密鑰

這裏是我的代碼:

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"); 
} 
} 

當我運行這段代碼我得到:此操作

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

需要當我刪除嘗試

Exception in thread "main" com.google.gdata.util.ServiceForbiddenException: Developer key required for this operation 

的GData ServiceForbiddenException開發關鍵{} catch代碼塊「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)... 

我在這裏錯過了什麼?我從谷歌文檔中得到了這段代碼。 我是否需要授予訪問我的帳戶的地方?如果是這樣,請解釋一下,因爲我無法在Google文檔中找到它。

+0

我修好了。問題是我注入價值的方式: String apiKey =「real-dev-api-key」; Ayway,現在這個工作。更新的思想。 – aki

回答

0

正如我在我的評論中提到的,我以錯誤的方式設置了propertie的值,String apiKey =「real-dev-api-key」。這是固定的,現在一切正常。

相關問題