【问题标题】:The YouTube account used to authorize the API request must be merged with the user's Google account用于授权 API 请求的 YouTube 帐户必须与用户的 Google 帐户合并
【发布时间】:2021-07-16 11:47:10
【问题描述】:

我知道之前有人问过这个问题,并且有一个经过验证的答案here,但那个灵魂对我不起作用。 我想从我的自定义软件回复我频道的 cmets。首先,我在 google 控制台中创建了一个服务帐户并创建了一个密钥并下载了一个包含该密钥的文件(请参阅下面代码中的 file.json)。我使用代码获得了我的 Oauth2.0 令牌:

try {
        GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("file.json"))
                .createScoped(Collections.singleton(YouTubeScopes.YOUTUBE_FORCE_SSL));
        String token = credentials.refreshAccessToken().getTokenValue();

    } catch (IOException e) {
        e.printStackTrace();
    }

file.json 文件来自 google 服务帐户控制台。它包括私钥、client_id、客户电子邮件和其他详细信息。这段代码一切正常,我成功收到了访问令牌。但是当我尝试向https://youtube.googleapis.com/youtube/v3/comments?part=snippet 发送一个http POST 请求时,需要解释here 的json 正文,我收到以下错误:

{
"error": {
    "code": 403,
    "message": "The YouTube account used to authorize the API request must be merged with the user's Google account.",
    "errors": [
        {
            "message": "The YouTube account used to authorize the API request must be merged with the user's Google account.",
            "domain": "youtube.comment",
            "reason": "ineligibleAccount",
            "location": "Authorization",
            "locationType": "header"
        }
    ]
}

}

提前致谢。

【问题讨论】:

    标签: java youtube-api youtube-data-api google-api-java-client service-accounts


    【解决方案1】:

    我在 google 控制台中创建了一个服务帐户

    YouTube API 不支持服务帐户身份验证。创建桌面应用凭据并使用 Oauth2 登录。

    已安装应用的代码应该是这样的。

     /**
       * Initializes an authorized YouTube service object.
       *
       * @return The YouTube  service object.
       * @throws IOException
       * @throws GeneralSecurityException
       */
      private static YouTube initializeYouTube() 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();
      }
    

    来自 cmets

    我发现如果没有向用户显示一些弹出窗口,就无法对 youtube api 进行身份验证,因为使用 Oauth 2.0 登录和您的示例都要求用户选择帐户以在某些弹出窗口中进行身份验证。

    不,YouTube API 没有其他选择。

    为了澄清我自己,我希望在幕后进行身份验证,并且用户可以通过验证预定义的凭据来发送回复。

    我已经这样做了,然后您可以在存储刷新令牌后授权您的应用程序,然后使用刷新令牌访问 API。

    我想知道这是否可能。

    至少同意授权一次。

    【讨论】:

    • YouTube API 不支持服务帐户身份验证。创建桌面应用程序凭据并使用 Oauth2 登录。 我看到如果没有向用户显示一些弹出窗口,就无法验证 youtube api,因为使用 Oauth 2.0 的登录和您的示例都要求用户选择帐户以在某些情况下进行身份验证弹出窗口。只是为了澄清我自己,我希望在幕后进行身份验证,并且用户可以通过验证预定义的凭据来发送回复。我想知道这是否可能。
    • 是的,Oauth2 确实需要用户同意。我了解您“想要”做什么,但问题是此 API 中不允许这样做。您可以做的是请求授权一次,然后使用刷新令牌访问 api。不幸的是,这是您使用 YouTube api 访问私人用户数据的唯一选择。您必须至少使用一次 Oauth2 并请求用户同意。
    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 2012-11-27
    • 2013-08-21
    • 2013-07-23
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多