YouTube 数据 API 3 速率端点是否存在时间滞后?

Is there a time lag in the YouTube Data API 3 rate endpoint?

我有一个显示 YouTube 视频的应用程序,它有一个评分按钮,允许用户喜欢或不喜欢该视频。在单击事件中,3 个函数通过 ajax 的 success 函数被链接在一起调用。流程是这样的:ytvRate() -> getRating() -> showRating()

当我记录操作和结果时,来自 getRating() 的响应没有我在 ytvRate() 中发送的值。如果我稍等片刻并刷新页面,getRating() 的结果就会恢复正确。我在 ytvRate() 中的 ajax 的成功函数中调用了 getRating()。这是否意味着在收到成功响应之前不应调用该函数?

这是我的日志示例:

rating sent: like
call get rating
this is my rating: none
call show rating

如您所见,从 API 编辑的评分 return 不正确 - 它应该是我刚刚发送的评分。刷新后,相同的调用 return 会得到正确的评级...那么,更新正确信息的数据 api 是否有延迟或其他原因?如何在发送请求的同一按钮点击上获得正确的评级?

以下是函数(showRating 似乎与问题无关。只要获得正确的评级,它就可以正常工作 - 但事实并非如此。)

function ytvRate(id, rating, event){
    event.preventDefault()

    var apiKey = 'This is a valid key';
    var client_id = 'This is a valid client id';
    var redirect_uri = 'This is a redirect uri';
    var scope = 'https://www.googleapis.com/auth/youtube';
    var rateUrl = 'https://www.googleapis.com/youtube/v3/videos/rate?id='+id+'&key='+apiKey+'&rating='+rating;

    if(getHash().access_token){
        var token = getHash().access_token;
        $.ajax({
            type: "POST",
            url: rateUrl,
            beforeSend: function (request){
                request.setRequestHeader('Authorization', 'Bearer ' + token);
            },
            success: function(data){
                console.log('rating sent: '+rating); 
                getRating(id);
            },
            error: function(e) {
                console.log(e);
            }
         });

    } else{
        window.location = 'https://accounts.google.com/o/oauth2/v2/auth?client_id='+client_id+'&redirect_uri='+redirect_uri+'&scope='+scope+'&response_type=token&prompt=consent&include_granted_scopes=false';
    }
    return false;
}

function getRating(id){
    var getRatingUrl = 'https://www.googleapis.com/youtube/v3/videos/getRating?id='+id;
    console.log('call get rating');
    if(getHash().access_token){
        var token = getHash().access_token;
        $.ajax({
            type: "GET",
            url: getRatingUrl,
            beforeSend: function (request){
                request.setRequestHeader('Authorization', 'Bearer ' + token);
            },
            success: function(data){
                var rating = data.items[0].rating;
                console.log("this is my rating: " + rating);
                showRating(rating, id);
            }
         });
    }
}

function showRating(response, id){
    console.log('call show rating');
    numLikes(id);
    if(response == 'like'){
        document.getElementById("notliked").className = "hide";
        document.getElementById("liked").className = "";
        document.getElementById("like-btn").style.color = "#87CEFA";
        document.getElementById("like-btn").setAttribute("onclick", "ytvRate('"+id+"', 'none', event)");
    } else{
        document.getElementById("notliked").className = "";
        document.getElementById("liked").className = "hide";
        document.getElementById("like-btn").style.color = "inherit";
        document.getElementById("like-btn").setAttribute("onclick", "ytvRate('"+id+"', 'like', event)");
    }
}

编辑:

有趣的是,如果我在新方法中调用 youtube/v3/videos 资源而不是 youtube/v3/videos/getRating 并访问 statistics.likeCount,该数字会立即更新。为什么我收不到同样效率的用户评价?

在评论中讨论后,我建议您采取不同的方法。当 ytvRate 成功时,您不需要获取 getRating,因为您已经知道用户设置的评级是什么。

rate 方法就像常规编程语言中的 setter - 如果它成功(没有抛出异常或返回错误),您可以假设当前值就是您想要的值设置而不再次获取它。这在 multithreaded/distributed 环境中可能是错误的假设,但在您的情况下可能没问题。

function ytvRate(id, rating, event){
    ...
    success: function(data){
        console.log('rating sent: '+rating); 
        showRating(rating, id);
    }
    ...
}