首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >用于授权API请求的YouTube帐户必须与用户的Google帐户合并

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

Stack Overflow用户
提问于 2021-04-22 01:03:16
回答 1查看 330关注 0票数 1

我知道以前有人问过这个问题,并且有一个经过验证的答案,这里,但是这个问题对我来说不管用。我想回复我的频道的评论,从我的定制软件。首先,我在google中创建了一个服务帐户,并创建了一个密钥,并下载了一个包含该键的文件(参见下面代码中的file.json )。我通过使用代码获得了Oauth2.0令牌:

代码语言:javascript
运行
AI代码解释
复制
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、客户端电子邮件和其他细节。这段代码一切正常,我成功地接收了访问令牌。但是,当我试图用所需的json向https://youtube.googleapis.com/youtube/v3/comments?part=snippet发送http请求时,这里解释说,我得到了以下错误:

代码语言:javascript
运行
AI代码解释
复制
{
"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"
        }
    ]
}

}

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-22 02:13:50

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

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

安装的应用程序的代码应该如下所示。

代码语言:javascript
运行
AI代码解释
复制
 /**
   * 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();
  }

从评论中

我发现在用户出现一些弹出的情况下无法对youtube api进行身份验证,因为OAuth2.0的登录和您的示例都要求用户在某些弹出窗口中选择帐户进行身份验证。

不,在YouTube API中没有其他选项。

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

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

我想知道这是否可能。

至少在未经授权一次的情况下不会。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67210122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档