我正在尝试使用服务帐户委派设置youtube live streaming API。我已经在我的域上完成了服务帐户委派的所有必要步骤。我有一个youtube频道拥有的用户与该域名的电子邮件id。Channel有超过10k的订阅者,并拥有直播的所有必要权限(我可以通过youtube studio直播)。在使用youtube java API时,我收到以下错误:
{
"code" : 403,
"errors" : [ {
"domain" : "youtube.liveBroadcast",
"message" : "The user is blocked from live streaming.",
"reason" : "livePermissionBlocked",
"extendedHelp" : "https://support.google.com/youtube/answer/2853834"
} ],
"message" : "The user is blocked from live streaming."
}我附上我的代码以供参考
private static final Collection<String> SCOPES =
Arrays.asList("https://www.googleapis.com/auth/youtube.force-ssl");
private static final String APPLICATION_NAME = "youtubeLive";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/**
* Create an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize(final NetHttpTransport httpTransport) throws IOException, GeneralSecurityException {
// Load client secrets.
InputStream in = new FileInputStream(CLIENT_SECRETS);
// GoogleClientSecrets clientSecrets =
// GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// // Build flow and trigger user authorization request.
// GoogleAuthorizationCodeFlow flow =
// new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
// .build();
// Credential credential = new AuthorizationCodeInstalledApp(flow, null).authorize("user");
// GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(CLIENT_SECRETS))
// .createScoped(SCOPES);
GoogleCredential credential = new GoogleCredential.Builder()
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new File("/Users/ashishjindal/Downloads/youtubelive-291213-be89f6b31144.p12"))
.setServiceAccountId("a******@youtubelive****.iam.gserviceaccount.com")
.setServiceAccountUser("USER@MY_DOMAIN.COM")
.setJsonFactory(JSON_FACTORY)
.setTransport(httpTransport)
.build();
return credential;
}
/**
* 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();
Credential credential = authorize(httpTransport);
return new YouTube.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
@Scheduled(fixedDelay = 1000l)
public void test() throws IOException, GeneralSecurityException {
YouTube youtubeService = getService();
LiveBroadcast liveBroadcast = new LiveBroadcast();
// Add the contentDetails object property to the LiveBroadcast object.
LiveBroadcastContentDetails contentDetails = new LiveBroadcastContentDetails();
contentDetails.setEnableClosedCaptions(true);
contentDetails.setEnableContentEncryption(true);
contentDetails.setEnableDvr(true);
contentDetails.setEnableEmbed(true);
contentDetails.setEnableAutoStart(true);
contentDetails.setRecordFromStart(true);
contentDetails.setStartWithSlate(true);
liveBroadcast.setContentDetails(contentDetails);
LiveBroadcastSnippet snippet = new LiveBroadcastSnippet();
snippet.setScheduledEndTime(new DateTime("2030-10-10T00:00:00"));
snippet.setScheduledStartTime(new DateTime("2029-10-10T00:00:00"));
snippet.setTitle("Test broadcast");
snippet.setChannelId("CHANNEL ID OWNED BY MY USER");
liveBroadcast.setSnippet(snippet);
LiveBroadcastStatus status = new LiveBroadcastStatus();
status.setPrivacyStatus("unlisted");
liveBroadcast.setStatus(status);
YouTube.LiveBroadcasts.Insert request = youtubeService.liveBroadcasts()
.insert("snippet,contentDetails,status", liveBroadcast);
liveBroadcast = request.execute();
System.out.println(response);
}编辑:我已经找到了根本原因。我正在传递属于一个品牌账户的频道Id,该品牌账户进一步归该用户所有。一个用户可以在youtube上拥有多个品牌帐户。现在我的新问题是如何使用service-account模拟品牌帐户。我发现了一些有用的线程:
发布于 2020-10-16 19:50:53
Google不支持服务帐户(与其他YouTube API一样)。
这里引用了官方API文档Move from ClientLogin to OAuth 2.0中的Service Accounts do not work with the YouTube API部分(下面的重点是我的):
服务帐户不适用于YouTube数据应用编程接口调用,因为服务帐户需要关联的YouTube通道,并且您无法将新的或现有的通道与服务帐户关联。如果您使用服务账号调用YouTube数据接口,接口服务端会returns an error,错误类型设置为
unauthorized,原因设置为youtubeSignupRequired。
此外,这是从official docs的另一个地方提取的文本,它再次陈述了这个事实(下面的重点也是我的):
如果您尝试使用OAuth 2.0服务帐户流,则通常会出现此错误。YouTube不支持服务帐户,如果您尝试使用服务帐户进行身份验证,则会出现此错误。
我从中提取上述官方声明的上下文并不包含在您上面显示的上下文中。尽管如此,事实仍然存在。
又一次引用了上面提到的事实,这一次来自Live Streaming API docs自己(下面的重点也是我的):
服务帐户流支持不访问用户信息的服务器到服务器交互。但是,YouTube直播应用编程接口不支持此流。由于无法将服务帐户链接到YouTube帐户,因此尝试使用此流授权请求将生成
NoLinkedYouTubeAccount错误。
因此,您必须承认,您不能通过服务帐户使用YouTube相应的API发起直播。
https://stackoverflow.com/questions/64387915
复制相似问题