用于授权 API 请求的 YouTube 帐户必须与用户的 Google 帐户合并

The YouTube account used to authorize the API request must be merged with the user's Google account

我知道这个问题之前有人问过,并且有一个经过验证的答案 但是那个灵魂对我不起作用。 我想通过我的自定义软件回复我频道的评论。首先,我在 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 with required json body explained here 发送 http POST 请求时,出现以下错误:

{
"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"
        }
    ]
}

}

提前致谢。

I created a service account in google console

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

来自评论

I see that there's no way to authenticate youtube api without some popup appearing to user because both the login with Oauth 2.0 and your example require user to choose account to authenticate in some popup.

不,YouTube 没有其他选择 API。

Just to clarify myself, I wanted the authentication to happen behind the scenes and user can send reply by authenticating pre-defined credentials.

我已完成此操作,您可以在存储刷新令牌后授权您的应用程序,然后在将来使用刷新令牌访问 API。

I was wondering if that would be possible.

不是没有至少同意一次授权。