当前位置: 首页 > 知识库问答 >
问题:

Youtube数据API-HTTP/1.1 401未经授权

慎建本
2023-03-14

当我想使用YouTube数据API上传视频时,它抛出了EXEPOPTION 401,但是我的凭据的范围是

upload("`https://www.googleapis.com/auth/youtube.upload`").

我已经在Google Developer Console中启用了API。我怎么才能解决这个问题,我很困惑。

2020-06-18 17:28:18.007信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:

--------------请求--------------

POST<代码>https://accounts.google.com/o/oauth2/token

接受编码:gzip

用户代理:Google HTTP Java客户端/1.23.0(gzip)

内容类型:application/x-www-form-urlencoded;字符集=UTF-8

内容长度:269

2020-06-18 17:28:18.007信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:curl-v-compressed-X POST-H“接受编码:gzip”-H“用户代理:Google HTTP Java客户端/1.23.0(gzip)”-H“内容类型:应用程序/X-www-form-urlencoded;字符集=UTF-8'-d'@-'--'<代码>https://accounts.google.com/o/oauth2/token '

2020-06-18 17:28:18.929信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:总计:269字节

2020-06-18 17:28:18.929信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:grant\u type=刷新\u令牌

2020-06-18 17:28:19.153信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:-------响应--------------

HTTP/1.1 200正常

传输编码:分块

X帧选项:SameOrgin

Alt Svc:h3-28=“:443”;ma=2592000,h3-27=“:443”;ma=2592000,h3-25=“:443”;ma=2592000,h3-T050=“:443”;ma=2592000,h3-Q050=“:443”;ma=2592000,h3-Q049=“:443”;ma=2592000,h3-Q048=“:443”;ma=2592000,h3-Q046=“:443”;ma=2592000,h3-Q043=“:443”;ma=2592000,quic=“:443”;ma=2592000;v=“46,43”

缓存控制:专用服务器:HTTPServer2上的脚手架X-Content-Type-Options:nosniff内容编码:gzip-Vary:Referer-Vary:X-Origin-Vary:Origin X-XSS-Protection:0日期:2020年6月18日星期四09:28:19 GMT内容类型:应用程序/json;字符集=utf-8

2020-06-18 17:28:19.175信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:总计:585字节

2020-06-18 17:28:19.176信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:{“access\u token”:“…”,“expires\u in”:3599,“scope”:“<代码>https://www.googleapis.com/auth/youtube **https://www.googleapis.com/auth/youtube.upload** https://www.googleapis.com/auth/content https://www.googleapis.com/auth/siteverification https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/siteverification.verify_only https://www.googleapis.com/auth/manufacturercenter“,”token\u type“:”Bearer“}

2020-06-18 17:28:22.490INFO 26088---[main]c.google.api.client.http.HttpTransfer:总计:144字节

2020-06-18 17:28:22.492信息26088---[main]c.google.api.client.http.HttpTransfer:{"片段":{"分类ID":"22","描述":"上传视频的描述.","标题":"测试视频上传."},"状态":{"隐私状态":"私有"}}

2020-06-18 17:28:22.493信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:-------请求--------------

发布

https://www.googleapis.com/upload/youtube/v3/videos?access_token=...&part=snippet&part=status&uploadType=resumable

接受编码:gzip

用户代理:ADAM Google API Java客户端/1.30.9 Google HTTP Java客户端/1.23.0(gzip)

x-goog-api-client:gl java/1.8.0 gdcl/1.30.9 windows-10/10.0

x-upload-content-type:应用程序/八位字节流

x-上传-内容-长度:4969741

Content-Type:应用程序/json;字符集=UTF-8

内容编码:gzip

内容长度:125

2020-06-18 17:28:22.494INFO 26088---[main]c.google.api.client.http.HttpTransfer: curl-v--压缩-X POST-H'Accept-编码: gzip'-H'User-Agent: ADAM Google-API-Java-客户端/1.30.9 Google-HTTP-Java-客户端/1.23.0(gzip)'-H'x-goog-api-客户端: gl-java/1.8.0 gdcl/1.30.9 windows-10/10.0'-H'x-上传-内容-类型:应用程序/ocet-stream'-H'x-上传-内容-长度:4969741'-H'Content-Type: Application/json; charset=UTF-8'-H'Content-编码: gzip'-d '@-' -- '<代码>https://www.googleapis.com/upload/youtube/v3/videos?access_token=...

2020-06-18 17:28:22.900信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:总计:144字节

2020-06-18 17:28:22.902信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:{“snippet”:{“categoryId”:“22”,“description”:“上传视频的描述”,“title”:“测试视频上传。”},“status”:{“privacyStatus”:“private”}}

2020-06-18 17:28:23.322信息26088---[主]c.google。应用程序编程接口。客户http。HttpTransport:-------响应--------------

HTTP/1.1 401未经授权

传输编码:分块

警告:214 UploadServer gzip已上载

Alt Svc:h3-28=“:443”;ma=2592000,h3-27=“:443”;ma=2592000,h3-25=“:443”;ma=2592000,h3-T050=“:443”;ma=2592000,h3-Q050=“:443”;ma=2592000,h3-Q049=“:443”;ma=2592000,h3-Q048=“:443”;ma=2592000,h3-Q046=“:443”;ma=2592000,h3-Q043=“:443”;ma=2592000,quic=“:443”;ma=2592000;v=“46,43”

缓存控制:专用

服务器:上传服务器

内容编码:gzip

X-GUploader-UploadID: AAANsUn5M53nz45hqEKbbVNqSMaAIK9KOfY53I0TeGum-0GkPUE9YkgpeF4tDIL3x973oQwG_JXGnWljYPH9uQBQFQ

更改:参考

变化:X-Origin

更改:原点

日期:2020年6月18日星期四09:28:23 GMT

Content-Type:应用程序/json;字符集=UTF-8

// You need to set this value for your code to compile.
    // For example: ... DEVELOPER_KEY = "YOUR ACTUAL KEY";
    private static final String DEVELOPER_KEY = "...";
    /**
     * 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();
    }
    private static final Collection<String> SCOPES =
            Arrays.asList("https://www.googleapis.com/auth/youtube.upload");

    private static final String APPLICATION_NAME = "ADAM";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();



    @Test
    public void test1()
            throws GeneralSecurityException, IOException, GoogleJsonResponseException {
        YouTube youtubeService = getService();
        Credential credential = GoogleAuthenticationUtil.getCredential("...");
        // Define the Video object, which will be uploaded as the request body.
        Video video = new Video();
        String accessToken = credential.getAccessToken();
        // 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);

        // TODO: For this request to work, you must replace "YOUR_FILE"
        //       with a pointer to the actual file you are uploading.
        //       The maximum file size for this operation is 137438953472.
        File mediaFile = new File("E:\\57086854184752465.mp4");
        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(Arrays.asList("snippet", "status"), video, mediaContent);
        Video response = request.setAccessToken(accessToken).execute();
        System.out.println(response);
    }

共有1个答案

隆璞
2023-03-14

我看不到您正在加载客户端机密json文件的位置。您可能只加载一个api密钥。

  /**
   * Initializes an authorized YouTube service object.
   *
   * @return The YouTube service object.
   * @throws IOException
   * @throws GeneralSecurityException
   */
  private static YouTube initializeAnalyticsReporting() throws GeneralSecurityException, IOException {

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

    // Load client secrets.
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(HelloYouTube.class
            .getResourceAsStream(CLIENT_SECRET_JSON_RESOURCE)));

    // Set up authorization code flow for all authorization scopes.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow
        .Builder(httpTransport, JSON_FACTORY, clientSecrets,
            YouTubeScopes.all()).setDataStoreFactory(dataStoreFactory)
        .build();

    // Authorize.
    Credential credential = new AuthorizationCodeInstalledApp(flow,
        new LocalServerReceiver()).authorize("user");
    // Construct the YouTube service object.
    return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APPLICATION_NAME).build();
  }
 类似资料:
  • 我正在使用YouTube数据API (Java)将视频上传到我的YouTube频道。我已经在我的Windows PC上测试过了,并且成功了。但是示例中的授权通过打开浏览器窗口登录Google来创建凭证实例。这在我的Windows PC上没问题,但是我试图让这段代码在我只能通过SSH访问的远程linux机器上运行。 我在stackoverflow中搜索了类似的问题,发现了完全相同的问题。但由于这个问

  • 我必须实现一种将视频从我们的网站上传到youtube的方法。我已经在Google Cloud中注册了该应用程序,并获得了所有必要的客户端ID、客户端密码、浏览器密钥、重定向uri和服务器密钥。我还按照不同网站的建议打开了YouTubeData API V3、Google API、Freebase API和YouTubeAnalytics API。 下面是我的代码: 我引用这些代码:使用YouTub

  • 我正在尝试使用Oauth和yt API。我想试试youtube的基于浏览器的上传。但首先,我必须让用户能够允许程序访问我的页面。https://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading#Browser_based_uploading 但首先我需要获取一个ACCESS_TOK

  • 我一直在使用YouTube Analytics API(我正在使用Java库)来检索YouTube频道的分析数据-我一直在过滤对特定VideoID的API调用。 然而,当我在YouTube Analytics(网络界面)上查看时,同一日期范围的数据似乎有所不同。 有人对此有解释吗?

  • 当构建一个不使用HTTP基本身份验证(但是像api-key这样的其他东西)并且客户机提供无效凭据的REST API时,您应该返回什么HTTP状态代码?401未经授权还是403禁止? IANA HTTP状态代码注册表将RFC7235第3.1节列为“401未经授权”的责任人,其中指出: HTTP 401响应必须始终包含一个WWW-Authenticate标头,该标头指示客户端如何进行身份验证。HTTP

  • 我正在建立一个应用程序,两个不同的用户将编辑相同的文件在线,只使用纯文本。为此,我使用GitHub上的google-api-php-client-master。 有一些例子,但我不明白我需要做什么才能达到目的。官方文档是不推荐的,因为它是一个不在线的库,所有的类都有不同的名称和工作方式。 我已经获得了我的凭据,并知道如何获得托管在我的驱动器帐户上的文档列表。但现在我需要: null 我不指望任何人