*401未经授权*当遵循教程Java实现视频.通过developers.google.com插入 &



我试图使用youtube数据API将mp4文件上传到我的youtube频道,但是,我一直得到401未经授权的回复。

使用google控制台云,我创建了一个(不受限制的)API密钥。之后,我启用了YouTube Data API v3然后,我从制作视频的用例文档中复制了java代码片段。插入调用他们的服务。

我从代码片段(required)更改的内容:

我使用前面提到的API密钥来替换"YOUR_API_KEY"也取代了new File("YOUR_FILE"),原因很明显。文件是一个17MB的mp4文件。

因为与代码片段一起提供的包不再附带com.google.api.client.json.jackson2.JacksonFactory;。在这个解决方案之后,我包含了依赖项:

<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.29.2</version>
</dependency>

new JacksonFactory()代替JacksonFactory.getDefaultInstance()

最后,由于youtubeService.videos().insert(args)不支持字符串作为其第一个参数,我将字符串替换为CSV字符串的List.of()。我还尝试了一个列表,每个逗号分隔的字符串作为列表中的项目,但这似乎也没有改变结果。

这会导致以下请求:https://youtube.googleapis.com/upload/youtube/v3/videos?key=MyKeyAware&part=snippet&part=status&uploadType=resumable

为了更好地衡量,以下是我使用的相关依赖项(不包括前面提到的依赖项):

<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.35.1</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>v3-rev20220612-1.32.1</version>
</dependency>

我真的很感激一些帮助,因为我一直在努力解决这个问题一段时间了!

代码:

package nl.phi.ysg.service.YoutubeApi;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
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.jackson.JacksonFactory;
import com.google.api.services.youtube.YouTube;
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 org.springframework.core.io.ClassPathResource;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
public class YoutubeApiServiceImpl implements YoutubeApiService {
private static final String DEVELOPER_KEY = "...";
private static final String APPLICATION_NAME = "YSG";
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
/**
* Build and return an authorized API client service.
*
* @return an authorized API client service
* @throws GeneralSecurityException, IOException
*/
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
return new YouTube.Builder(httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME).build();
}

public static void main(String[] args) throws GeneralSecurityException, IOException, GoogleJsonResponseException {
YouTube youtubeService = getService();

// Define the Video object, which will be uploaded as the request body.
Video video = new Video();
// Add the snippet object property to the Video object.
VideoSnippet snippet = new VideoSnippet();
snippet.setCategoryId("22");
snippet.setDescription("Description of uploaded video.");
snippet.setTitle("Test video upload.");
video.setSnippet(snippet);
// Add the status object property to the Video object.
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("private");
video.setStatus(status);
File mediaFile = new ClassPathResource("test/test.mp4").getFile();
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
// Define and execute the API request
YouTube.Videos.Insert request = youtubeService.videos().insert(List.of("snippet,status"), video, mediaContent);
Video response = request.setKey(DEVELOPER_KEY).execute();
System.out.println(response);
}
}

你的问题不清楚,但错误表明你不包括用户身份验证(OAuth),这是YouTube API所要求的:

参见快速入门,具体在步骤2b中创建OAuth客户端ID。

API密钥用于验证您的应用程序。

OAuth2客户端ID用于验证人类用户。

最新更新