问题
I've got a problem that I cannot solve. I want to allow users to connect to multiple youtube channels at once and create youtube streams for them. The main code looks like this:
export const setGapiTokenForTarget = async (token) => {
await window.gapi.client.setToken(token);
await window.gapi.auth.setToken(token);
};
export const bindStreamToBroadcast = async (streamId, broadcastId, token) => {
await setGapiTokenForTarget(token);
return window.gapi.client.youtube.liveBroadcasts.bind({
'id': broadcastId,
'part': 'snippet',
'streamId': streamId
});
};
export const createBroadcast = async (token) => {
await setGapiTokenForTarget(token);
return window.gapi.client.youtube.liveBroadcasts.insert({
'part': 'snippet,contentDetails,status',
'resource': {
'snippet': {
'title': 'l1',
'scheduledStartTime': '2020-02-07T09:20:17.039Z',
'scheduledEndTime': '2020-02-07T09:40:17.039Z'
},
'status': {
'privacyStatus': 'unlisted'
}
}
});
};
export const createStream = async (token) => {
await setGapiTokenForTarget(token);
return window.gapi.client.youtube.liveStreams.insert({
'part': 'snippet,cdn,contentDetails,status',
'resource': {
'snippet': {
'title': 'LIVE PANEL TEST',
'description': 'LIVE PANEL TEST'
},
'cdn': {
'frameRate': '60fps',
'ingestionType': 'rtmp',
'resolution': '1080p'
},
'contentDetails': {
'isReusable': true
}
}
});
};
export const createStreamAndBroadcastForTarget = async (target) => {
const broadcast = await createBroadcast(target.token);
const stream = await createStream(target.token);
const binded = await bindStreamToBroadcast(stream.result.id, broadcast.result.id, target.token);
return { stream, broadcast, binded };
};
export const createStreamAndBroadcastForTargets = async (targets, { ...all }) => {
const youtubeEvents = targets.map(createStreamAndBroadcastForTarget);
return Promise.all(youtubeEvents);
};
This is just the prototype and the data is hard-coded. However, when I try to setToken
for each target (channel), the profile doesn't change and I'm doing the same thing (creating lives) at only one of the channels. Each channel has it's own token
returned from google after sign in and it is an object taken directly from google, not just key (string). I've checked it and every token is different for each channel. It looks like window.gapi.client.setToken
and window.gapi.auth.setToken
is not working and it doesn't switch anything.
回答1:
The YouTube API is channel based not user based. Your going to have to logout a user and log them in under a different channel.
Your going to have to have a live token for each channel in your script if you want to have access to more then on at a time and im not sure that library supports that.
来源:https://stackoverflow.com/questions/60111548/settoken-doesnt-switch-youtube-channel-in-google-api-gapi