如何订阅youtube api?

时间:2018-05-03 05:21:06

标签: node.js youtube

https://developers.google.com/youtube/v3/code_samples/apps-script#subscribe_to_channel

您好,

我无法想象如何订阅带有帖子请求的YouTube频道。我不打算使用如上所示的YoutubeSubscriptions。我很简单地想要传递api密钥,但似乎无法弄明白。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果您不想使用YouTube订阅,则必须在登录YouTube帐户后获得session_token

session_token存储在隐藏的输入标签中: document.querySelector('input[name=session_token]').value

或全文本搜索XSRF_TOKEN字段,对应值为session_token,参考常规: const regex = /\'XSRF_TOKEN\':(.*?)\"(.*?)\"/g

以下是Python中的实现:

def YouTubeSubscribe(url,SessionManager):
while(1):
    try:
        html = SessionManager.get(url).content
        session_token = (re.findall("XSRF_TOKEN\W*(.*)=", html , re.IGNORECASE)[0]).split('"')[0]
        id_yt = url.replace("https://www.youtube.com/channel/","")
        params = (('name', 'subscribeEndpoint'),)
        data = [
          ('sej', '{"clickTrackingParams":"","commandMetadata":{"webCommandMetadata":{"url":"/service_ajax","sendPost":true}},"subscribeEndpoint":{"channelIds":["'+id_yt+'"],"params":"EgIIAg%3D%3D"}}'),
          ('session_token', session_token+"=="),
        ]
        response = SessionManager.post('https://www.youtube.com/service_ajax', params=params, data=data)
        check_state = json.loads(response.content)['code']
        if check_state == "SUCCESS":
            return 1
        else:
            return 0
    except Exception as e:
        print "[E] YouTubeSubscribe:"+ str(e)
        pass