gpt4 book ai didi

javascript - Angular/YouTube API v3 : 401 Invalid credentials error on search

转载 作者:行者123 更新时间:2023-12-03 06:21:37 30 4
gpt4 key购买 nike

还在学习 Angular,所以请温柔一点。 ;)

当我第一次创建并添加 YouTube API key 时,我成功获得了搜索结果。但是,第二天它停止工作并给我一个 401 错误,并显示“凭据无效”消息。

我在这里看到了一些关于必须添加 OAuth 的问题,但我认为这不适用于我,因为我只是在搜索和播放视频......对吗?

我一直在使用this project作为指南,它工作得很好。我不确定我做了什么不同的事情。

这是我的 Controller ,带有 $http.get搜索。

var youtubeAPIKey = "AIzaSyA4EF_e56jFBfRPBx57wkzN2ppKudMcZvk";

$scope.search = function() {
$http.get('https://www.googleapis.com/youtube/v3/search', {
params: {
key: youtubeAPIKey,
type: 'video',
maxResults: '20',
part: 'id,snippet',
videoCategoryId: '10',
q: this.query
}
})
.success(function(data) {
console.log(data);
$scope.searchResults = data;
})
.error(function(err) {
console.log(err);
});
}

不确定是否重要,但这是搜索 HTML
<div class="search-box">
<form ng-submit="search()">
<input type="search" placeholder="It's your turn! Search for a song to add"
ng-model="query" />
</form>
</div>
<div class="search-results" ng-if="searchResults">
<ul>
<li class="list-item"
ng-repeat="result in searchResults.items"
ng-click="add(result.id.videoId, result.snippet.title)">
<div class="song-info">{{result.snippet.title}}</div>
<button class="player-btn glyphicon glyphicon-headphones"
ng-click="preview(result.id.videoId, result.snippet.title)">
</button>
<button class="player-btn glyphicon glyphicon-plus"></button>
</li>
</ul>

编辑:这是错误:
error: Object
code: 401
errors: Array[1]
0: Object
domain: "global"
location: "Authorization"
locationType: "header"
message: "Invalid Credentials"

最佳答案

我会首先检查您是否可以通过 RESTful 控制台(例如 Postman)、CURL 或仅从浏览器发出请求。例如,从命令行运行:

$ curl -XGET https://www.googleapis.com/youtube/v3/search?key=<API_KEY_HERE>\&part=id,snippet

或者只是从浏览器:
https://www.googleapis.com/youtube/v3/search?key=<API_KEY_HERE>&part=id,snippet

这至少可以隔离问题是 Angular 还是端点/请求。

关于javascript - Angular/YouTube API v3 : 401 Invalid credentials error on search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247624/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com