Youtube search fail with GoogleJsonResponseExcepti

2019-07-20 10:00发布

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.

2条回答
smile是对你的礼貌
2楼-- · 2019-07-20 10:40

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.

查看更多
3楼-- · 2019-07-20 10:49

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

查看更多
登录 后发表回答