【发布时间】: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