Youtube search fail with GoogleJsonResponseExcepti

2019-07-20 10:12发布

问题:

This seems like a basic procedure but I don't know what's going wrong.

Error in logcat output:

com.google.api.client.googleapis.json.GoogleJsonResponseException:
403 Forbidden
{
"code" : 403, 02-26
"errors" : [ {
"domain" : "usageLimits",
"message" : "Access Not Configured",
"reason" : "accessNotConfigured"
}
"message" : "Access Not Configured"

I configured in the code.google.com API console the YouTube data API and got a simple access API key for my Android app.

The code is below; the same as the sample code for search:

YouTube youTube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY,
  new HttpRequestInitializer() {
    @Override
    public void initialize(HttpRequest httpRequest) throws IOException {}
})
.setApplicationName("youtubeplayer")
.setYouTubeRequestInitializer(new YouTubeRequestInitializer(DEV_KEY))
.build();

YouTube.Search.List search = youTube.search().list("id,snippet");
//search.setKey(DEV_KEY);
search.setQ(videoName);
search.setType("video");

search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEO);
SearchListResponse searchListResponse = search.execute();
List<SearchResult> searchResultList = searchListResponse.getItems();

This is for Android. I can play videos with the YouTube Android player API, using the same key.

回答1:

You need to use the Browser Key for accessing Youtube Data API. Android key is used only for Youtube Player.

So just generate the Browser key in the DevConsole and you are good to go.



回答2:

1) You need to get your SHA1 key with "keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android"

2) In API console: Create a client id with this key and full package name.

3) In API console: Create an Android key with this key and full package name.

4) Enable YouTube Data API v3 from console