2
我試圖使用Simple Access API(API Key)在我的YouTube帳戶中上傳視頻。錯誤「401未經授權」使用Simple Access API(API密鑰)在我的YouTube帳戶中上傳視頻
我得到這個異常。
You chose src/resources/video.avi to upload.
Initiation Started
Initiation Completed
Exception: 401 Unauthorized
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:143)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:115)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:421)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:340)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:458)
at com.google.api.services.samples.YoutubeSample.queryGoogleYouTube(YoutubeSample.java:126)
at com.google.api.services.samples.YoutubeSample.main(YoutubeSample.java:152)
出了什麼問題?
使用Simple API(API Key)和youtube-API的文檔非常差。 任何人都可以幫助我嗎?
我的簡單訪問API:
Simple API Access
Use API keys to identify your project when you do not need to access user data.
Key for server apps (with IP locking)
API key: AIzaSxxxxxxxxxxxxxxxxxxxxxxx-bVE8jRlzXY
IPs: Any IP allowed
Activated on: Feb 11, 2014 7:50 PM
Activated by: [email protected] – you
這是我的代碼。
package com.google.api.services.samples;
import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeRequestInitializer;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoSnippet;
import com.google.api.services.youtube.model.VideoStatus;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class YoutubeSample {
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static YouTube youtube;
private static String VIDEO_FILE_FORMAT = "video/*";
private static final String APPLICATION_NAME = "YoutubeTest";
//Simple API Access
//Use API keys to identify your project when you do not need to access user data.
//Key for server apps (with IP locking)
private static final String API_KEY = "AIzaSxxxxxxxxxxxxxxxxxx-bVE8jRlzXY"; // This has changed.
private static File getVideoFromUser() throws IOException {
return new File("src/resources/video.avi");
}
private static void queryGoogleYouTube() throws Exception {
ClientCredentials.errorIfNotSpecified();
try {
//In this piece of code that is my difficulty
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY,
new HttpRequestInitializer() {
public void initialize(HttpRequest request)
throws IOException {
}
})
.setApplicationName(APPLICATION_NAME)
.setYouTubeRequestInitializer(new YouTubeRequestInitializer(API_KEY)).build();
File videoFile = getVideoFromUser();
System.out.println("You chose " + videoFile + " to upload.");
Video videoObjectDefiningMetadata = new Video();
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoObjectDefiningMetadata.setStatus(status);
VideoSnippet snippet = new VideoSnippet();
Calendar cal = Calendar.getInstance();
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription("Video uploaded via YouTube Data API V3 using the Java library "
+ "on " + cal.getTime());
// Set your keywords.
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
videoObjectDefiningMetadata.setSnippet(snippet);
InputStreamContent mediaContent = new InputStreamContent(
VIDEO_FILE_FORMAT, new BufferedInputStream(
new FileInputStream(videoFile)));
mediaContent.setLength(videoFile.length());
YouTube.Videos.Insert videoInsert = youtube.videos().insert(
"snippet,statistics,status", videoObjectDefiningMetadata,
mediaContent);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
public void progressChanged(MediaHttpUploader uploader)
throws IOException {
switch (uploader.getUploadState()) {
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: "
+ uploader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Execute upload.
Video returnedVideo = videoInsert.execute();
// Print out returned results.
System.out
.println("\n================== Returned Video ==================\n");
System.out.println(" - Id: " + returnedVideo.getId());
System.out.println(" - Title: "
+ returnedVideo.getSnippet().getTitle());
System.out.println(" - Tags: "
+ returnedVideo.getSnippet().getTags());
System.out.println(" - Privacy Status: "
+ returnedVideo.getStatus().getPrivacyStatus());
System.out.println(" - Video Count: "
+ returnedVideo.getStatistics().getViewCount());
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
public static void main(String[] args) {
try {
queryGoogleYouTube();
} catch (Exception e) {
e.printStackTrace();
}
}
}
謝謝jlmcdonald ...我想我的網站用戶上傳一個視頻到我的YouTube帳戶。我的網站在我的YouTube帳戶中進行身份驗證併發送用戶的視頻(即沒有我的最終用戶的憑據)。有可能嗎? – gilbertoastolfi
否;用戶需要上傳到自己的帳戶,而不是你的帳戶。您的網站應該使用oAuth2通過他們的帳戶進行身份驗證。另一種可能是使用YouTube Lite;他們仍會上傳到自己的帳戶中,但它也會根據所有視頻在您的帳戶中創建一個自動播放列表。 – jlmcdonald