2015-12-13 122 views
0

仍在學習Angular,所以請溫和請。 ;)Angular/YouTube API v3:401搜索時無效憑證錯誤

我在第一次創建並添加YouTube API密鑰時成功獲取搜索結果。然而,第二天它停止工作,並給我一個401錯誤消息「無效憑證」。

我在這裏看到了一些關於添加OAuth的問題,但我不認爲這適用於我,因爲我只是在搜索和播放視頻......對嗎?

我一直在使用this project作爲指導,它工作得很好。我不確定我做了什麼不同。

這裏是我的控制器與$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" 

回答

0

我會先檢查你其實可以做請求通過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 

這將至少隔離問題是否與角或與端點/請求。

+0

我可以使用您發佈的網址返回結果,但如果我嘗試添加查詢或類別,例如:https://www.googleapis.com/youtube/v3/search?key = AIzaSyA4EF_e56jFBfRPBx57wkzN2ppKudMcZvk&part = id,snippet&q = kanye'我得到一個403錯誤消息,「在您的API密鑰上配置了每IP或per-Referer限制,並且請求與這些限制不匹配。如果應該允許來自此IP或引用者的請求,請使用Google Developers Console更新您的API密鑰配置.' – knowbody

+0

另外,我沒有HTTP引用者集。我試圖添加OAuth,但我得到了相同的「無效的憑據」錯誤。 – knowbody

+0

這聽起來像你還沒有設置適當的Youtube API密鑰(它看起來像你需要一個)。我建議設置一個,並使用新的API密鑰發出相同的請求,並查看是否可以通過該錯誤。 –