setToken doesn't switch YouTube channel in Google API (gapi)

血红的双手。 提交于 2020-02-27 12:50:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!