【问题标题】:Debug youtube data API? (rating)调试 youtube 数据 API? (评分)
【发布时间】:2021-05-17 03:19:46
【问题描述】:

我已复制并粘贴此处为 JS 提供的评级 API 代码。 https://developers.google.com/youtube/v3/docs/videos/rate?hl=es&apix_params=%7B%22id%22%3A%22O8IvzIs1s5Y%22%2C%22rating%22%3A%22none%22%7D&apix=true

几天前它工作了,但现在我无法让它工作。身份验证似乎正在发生,但评级部分不会改变评级也不会失败。我什至没有收到 2 条 console.log 消息。

我正在从 API 中授权的 localhost:8000 (http) 对其进行测试。

HTML相关代码为:

  <button type="submit" onclick="authenticate().then(loadClient)" class="btn btn-block btn-lg btn-primary">Authorize me!</button>
  <button type="submit" onclick="execute()" class="btn btn-block btn-lg btn-primary">Rate it!</button>

JS部分是:

  <!-- From: https://developers.google.com/youtube/v3/docs/videos/rate?apix=true#try-it -->
  <script src="https://apis.google.com/js/api.js"></script>
  <script>
    /**
     * Sample JavaScript code for youtube.videos.getRating
     * See instructions for running APIs Explorer code samples locally:
     * https://developers.google.com/explorer-help/guides/code_samples#javascript
     */

    function authenticate() {
      return gapi.auth2.getAuthInstance()
          .signIn({scope: "https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtubepartner"})
          .then(function() { console.log("Sign-in successful"); },
                function(err) { console.error("Error signing in", err); });
    }
    function loadClient() {
      gapi.client.setApiKey("MY API KEY");
      return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/youtube/v3/rest")
          .then(function() { console.log("GAPI client loaded for API"); },
                function(err) { console.error("Error loading GAPI client for API", err); });
    }
    // Make sure the client is loaded and sign-in is complete before calling this method.
    function execute() {
      console.log("Pasa")
      return gapi.client.youtube.videos.rate({
        "id": "VIDEOID to rate",
        "rating": "dislike"
      }).then(function(response) {
                  // Handle the results here (response.result has the parsed body).
                  console.log("Response", response);
      }, function(err) { console.error("Execute error", err); });
    }
    gapi.load("client:auth2", function() {
      gapi.auth2.init({client_id: "<CLIENT ID>"});
    });
  </script>

注意:我已经替换了凭据和 videoid。

已编辑:

@Nik,在 Firefox 中测试给了我这个:TypeError: gapi.client.youtube is undefined [了解更多] 家:286:7 执行 http://localhost:8000/home:286:7 onclick gapi 没有初始化?

我正在缩小范围。我的按钮在标题标签中的位置....将按钮从标题中移出它可以工作。有没有人对此有解释。 JS 变量是否有与 HTML 标签相关的 reduce 范围?

【问题讨论】:

  • 请问您遇到的错误是什么?
  • 更多信息:我在一个普通页面中进行了测试,没有其他 JS 代码(最近有 bootstrap 5 和 JQuery)并且工作正常。
  • 我没有收到任何错误,一堆与我不理解的 SameSite cookie 相关的“问题”。不确定这是否相关。
  • 我认为有一个错误,但只是出现一秒钟然后消失。可以看看如何获​​取。控制台中的最终输出:XHR 完成加载 GET 行

标签: javascript youtube-data-api


【解决方案1】:

实际上,它并没有失败,它正在对视频进行评级,但 API 没有发送任何响应。它正在发送状态204,这意味着:

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

注意:视频的ID应该是正确的

【讨论】:

  • 谢谢@Rahul!但我没有看到视频中的评级发生变化。一定是别的东西。
【解决方案2】:

很抱歉打扰您@Rahul Arora,@Nik。现在我知道出了什么问题。与 HEADER 标签无关。

我的按钮在 FORM 内!:

<form>
<button type="submit" onclick="authenticate().then(loadClient)" class="btn btn-block btn-lg btn-primary">Authorize me!</button>
  <button type="submit" onclick="execute()" class="btn btn-block btn-lg btn-primary">Rate it!</button>
</form>

并且表单已提交,因此在执行时丢失了 gapi 身份验证实例。

都是我的错!我改编了一个 HTML 模板,但没有意识到表单内的按钮。我发布这个以防其他人犯同样的愚蠢错误!

【讨论】:

    猜你喜欢
    • 2010-12-26
    • 2020-06-27
    • 2013-09-20
    • 2021-04-26
    • 2016-05-10
    • 2016-02-27
    • 2018-03-14
    • 2011-03-03
    • 2022-01-05
    相关资源
    最近更新 更多