* 401未经授权*遵循video.streser.com interive video.google.com的java实现时*

发布于 2025-02-09 15:14:53 字数 4942 浏览 2 评论 0原文

我正在尝试使用YouTube数据API将MP4文件上传到我的YouTube频道上,但是,我一直未经授权的401。

使用 Google Console Cloud 我创建了一个(不受限制的)API键。之后,我启用了 youtube数据API V3 然后,我从文档文档用于制作 video.intert 致电他们的服务。

我从摘要(必需)更改的内容:

我使用上述API键替换“ your_api_key” 并且出于明显的原因,还替换了新文件(“ your_file”)。该文件是17MB MP4文件。

因为带有片段提供的软件包不再带有com.google.api.client.json.jackson.jackson2.jacksonfactory;以下这个解决方案我包括了依赖性:

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

我替换了jacksonfactory.getDefaultInstance() new JacksonFactory(

) ().insert(args)不支持字符串,因为它的第一个参数我用CSV字符串的list.of()替换字符串。我还尝试了每个逗号分隔字符串作为列表中的项目的列表,但这似乎也不会改变结果。

这将导致以下请求:https://youtube.googleapis.com/upload/youtube/v3/videos?key=myKeyKeyaware&part=snippet = snippet&part = status&part = status&amp; amp; amp

; ,以下是我正在使用的相关依赖关系(不包括上述依赖关系):

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

}

I am trying to upload an mp4 file to my youtube channel using the YouTube Data API, however, I keep getting a 401 Unauthorized back.

Using the google console cloud I created an (unrestricted) API key. Afterwards, I enabled YouTube Data API v3
I then copied the java code snippet from the use cases documentation for making a Video.Insert call to their services.

The things I changed from the snippet (required):

I used the aforementioned API key to replace "YOUR_API_KEY"
And also replaced new File("YOUR_FILE") for obvious reasons. The file is a 17MB mp4 file.

Because the package provided with the snippet no longer comes with com.google.api.client.json.jackson2.JacksonFactory; Following this solution I included the dependency:

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

And I replaced JacksonFactory.getDefaultInstance() with new JacksonFactory()

Finally, as youtubeService.videos().insert(args) doesn't support a string as its first argument I replaced the string with a List.of() of the CSV string. I've also tried a List with each of the comma-seperated strings as items in the list but that doesn't seem to change the outcome either.

This results in the following request: https://youtube.googleapis.com/upload/youtube/v3/videos?key=MyKeyAware&part=snippet&part=status&uploadType=resumable

For good measure, here are the relevant dependencies I'm using (excluding the aforementioned dependency):

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

I would really appreciate some help as I've been struggling with this issue for a while now!

Code:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-02-16 15:14:54

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

请参阅 QuickStart 在步骤2B中专门创建OAuth客户端ID。

API密钥用于对您的应用进行身份验证。

OAuth2客户端ID用于对人类用户进行身份验证。

It's unclear from your question but the error indicates that you're not including User authentication (OAuth) and this is required by the YouTube API:

See the Quickstart specifically creating OAuth client ID in step 2b.

The API key is used to authenticate your app.

The OAuth2 client ID is used to authenticate human users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文