【问题标题】:YouTube Data API: add a subscriptionYouTube 数据 API:添加订阅
【发布时间】:2016-02-08 00:41:17
【问题描述】:

我正在使用 YouTube 的 V3 Data API 向频道添加订阅。这发生在 Wordpress 安装上。

我在 Wordpress 主题功能上添加了 Google API(用于 oauth):

wp_enqueue_script( 'googleapi', 'https://apis.google.com/js/client.js?onload=googleApiClientReady', array(), '1.0.0', true );

我以同样的方式添加了 oauth javascript 文件,这是这里的第一个文件:https://developers.google.com/youtube/v3/code_samples/javascript

按照本指南(https://developers.google.com/youtube/v3/docs/subscriptions/insert (Apps Script)),我使用 addSubscription 方法扩展了 OAuth js。

Google 客户端 API 似乎已加载并正常工作,因为它在 oauth javascript 上正确调用了 googleApiClientReady。

所以,这就是订阅的插入方式:

OAUTH JAVASCRIPT

... ... ...

// After the API loads
function handleAPILoaded() {
  addSubscription();
}
function addSubscription() {
  // Replace this channel ID with the channel ID you want to subscribe to
  var channelId = 'this is filled with the channel ID';
  var resource = {
    snippet: {
      resourceId: {
        kind: 'youtube#channel',
        channelId: channelId
      }
    }
  };

  try {
    var response = YouTube.Subscriptions.insert(resource, 'snippet');
    jQuery('#success').show();

  } catch (e) {
    if(e.message.match('subscriptionDuplicate')) {
      jQuery('#success').show();
    } else {
      jQuery('#fail').show();

      alert("Please send us a mail () with the following: ERROR: " + e.message);
    }

  }

所以,第一个错误是

YouTube.Subscriptions.insert(resource, 'snippet')

它说 YouTube 没有定义。我将其替换为:

gapi.client.youtube.subscriptions.insert(resource, 'snippet');

然后那个错误就消失了。检查响应时,由于订阅未完成,这就是我得到的

{"wc":1,"hg":{"Ph":null,"hg":{"path":"/youtube/v3/subscriptions","method":"POST","params":{},"headers":{},"body":"snippet","root":"https://www.googleapis.com"},"wc":"auto"}}

所以,我想知道那个 POST 请求发生了什么以及解决方案是什么。

我可以发布完整的 OAuth 文件,但就像在示例中一样,加上最后的 addSubscription 方法。

【问题讨论】:

    标签: youtube-data-api


    【解决方案1】:

    好的,我搞定了,问题出在 POST 请求上。这是完整的工作方法:

    // Subscribes the authorized user to the channel specified
    function addSubscription(channelSub) {
        var resource = {
            part: 'id,snippet',
            snippet: {
                resourceId: {
                    kind: 'youtube#channel',
                    channelId: channelSub
                }
            }
        };
    
        var request = gapi.client.youtube.subscriptions.insert(resource);
        request.execute(function (response) {
            var result = response.result;
            if (result) {
                // alert("Subscription completed");
                }
            } else {
                // alert("Subscripion failed");
                // ...
            }
        });
    }
    

    还要确保加载 Google Apps API(事实上,如果没有它,授权/登录按钮将不起作用)和 jQuery。

    【讨论】:

    • 我收到此错误:{ "error": { "code": 400, "message": "'snippet'", "errors": [ { "message": "'snippet'", "domain": "youtube.part", "reason": "unknownPart", "location": "part", "locationType": "parameter" } ] } }
    【解决方案2】:

    任何机会你都可以发布所有让这个工作的东西......所有 JS 整个 auth.js 保存为你的私钥,我正在解决这个确切的问题。

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2016-02-12
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      相关资源
      最近更新 更多