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

标签 java google-api youtube-data-api google-oauth2 google-api-java-client

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

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


我已经在Google Developer Console中启用了API。我该如何解决呢,我很困惑。



我的密码

2020-06-18 17:28:18.007信息26088 --- [main] c.google.api.client.http.HttpTransport:

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

开机自检https://accounts.google.com/o/oauth2/token

接受编码:gzip

用户代理:Google-HTTP-Java-Client / 1.23.0(gzip)

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

内容长度:269

2020-06-18 17:28:18.007信息26088 --- [main] c.google.api.client.http.HttpTransport:
curl -v --compressed -X POST -H'接受编码:gzip'-H'用户代理:Google-HTTP-Java-Client / 1.23.0(gzip)'-H'内容类型:application / x -www-form-urlencoded; charset = UTF-8'-d'@-'-
'https://accounts.google.com/o/oauth2/token'<< $$$

2020-06-18 17:28:18.929 INFO 26088 --- [main] c.google.api.client.http.HttpTransport:总计:269字节

2020-06-18 17:28:18.929信息26088 --- [main] c.google.api.client.http.HttpTransport:grant_type = refresh_token&refresh_token = ...&client_id = ...&client_secret = ...

2020-06-18 17:28:19.153 INFO 26088 --- [main] c.google.api.client.http.HttpTransport:
--------------回应--------------

HTTP / 1.1 200 OK

传输编码:分块

X-Frame-Options:SAMEORIGIN

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
变化:推荐人
变数:X-Origin
变化:起源
X-XSS-保护:0
日期:格林尼治标准时间2020年6月18日星期四09:28:19
内容类型:application / json;字符集= utf-8

2020-06-18 17:28:19.175信息26088 --- [main] c.google.api.client.http.HttpTransport:总计:585字节

2020-06-18 17:28:19.176信息26088 --- [main] c.google.api.client.http.HttpTransport:{
“ access_token”:“ ...”,
“ expires_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_type”:“承载者”
}

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

2020-06-18 17:28:22.492信息26088 --- [main] c.google.api.client.http.HttpTransport:{“ snippet”:{“ categoryId”:“ 22”,“ description”:“ Description的上传视频。“,”标题“:”测试视频上传。“},”状态“:{” privacyStatus“:”私人“}}

2020-06-18 17:28:22.493信息26088 --- [main] c.google.api.client.http.HttpTransport:
--------------请求--------------

开机自检

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


接受编码:gzip

用户代理:ADAM Google-API-Java-Client / 1.30.9 Google-HTTP-Java-Client / 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-upload-content-length:4969741

内容类型:application / json;字符集= UTF-8

内容编码:gzip

内容长度:125

2020-06-18 17:28:22.494信息26088 --- [main] c.google.api.client.http.HttpTransport:
curl -v --compressed -X POST -H'接受编码:gzip'-H'用户代理:ADAM Google-API-Java-Client / 1.30.9 Google-HTTP-Java-Client / 1.23.0(gzip )'-H'x-goog-api-client:gl-java / 1.8.0 gdcl / 1.30.9 windows-10 / 10.0'-H'x-upload-content-type:application / octet-stream'-H 'x-upload-content-length:4969741'-H'Content-Type:应用程序/ json; charset = UTF-8'-H'内容编码:gzip'-d'@-'
-'https://www.googleapis.com/upload/youtube/v3/videos?access_token=...&part=snippet&part=status&uploadType=resumable'<< $$$

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

2020-06-18 17:28:22.902信息26088 --- [main] c.google.api.client.http.HttpTransport:{“ snippet”:{“ categoryId”:“ 22”,“ description”:“ Description的上传视频。“,”标题“:”测试视频上传。“},”状态“:{” privacyStatus“:”私人“}}

2020-06-18 17:28:23.322信息26088 --- [main] c.google.api.client.http.HttpTransport:
--------------回应--------------

HTTP / 1.1 401未经授权

传输编码:分块

警告:214 UploadServer已压缩

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”

缓存控制:私有

伺服器:UploadServer

内容编码:gzip

X-GUploader-UploadID:AAANsUn5M53nz45hqEKbbVNqSMaAIK9KOfY53I0TeGum-0GkPUE9YkgpeF4tDIL3x973oQwG_JXGnWljYPH9uQBQFQ

变化:推荐人

变数:X-Origin

变化:起源

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

内容类型:application / 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);
    }

最佳答案

我看不到您要在哪里加载客户端机密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();
  }

关于java - YouTube数据API-HTTP/1.1 401未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62447359/

相关文章:

java - 为什么SortedSet中没有 "descendingIterator ()",但TreeSet中却有?

python - 如何使用 Google calendar api python 查找两个事件之间的时差

c# - 如何配置对 YouTube 凭据进行身份验证的网站以供控制台应用程序使用?

java - 每秒减少一个整数

java - Query#scroll() 在 Hibernate 中被弃用是因为什么?

java - 如何将文件添加到 jars META-INF

java - 如何在 Java/Android 中使用 OAuth 通过 Gmail 发送电子邮件?

javascript - 获取google面积图的H-Axis(X-Axis)值

ios - 按 channel 名称检索 YouTube channel 视频

youtube - YouTube数据API-查找购买的内容