Android - youtube api r52 - “访问未配置”

时间:2013-05-21 11:29:48

标签: android api-key android-youtube-api

我正在构建一个简单的测试Android应用程序,我正在尝试获取有关特定视频的数据。我已经使用了我的调试密钥库的SHA1指纹并用它创建了一个google api密钥(和我的包名)。我为它启动了youtube api服务。

这是我的代码:

YouTube youtube = new YouTube.Builder(new NetHttpTransport(), new GsonFactory(), new HttpRequestInitializer() {
    public void initialize(HttpRequest request) throws IOException {}
}).setApplicationName("MyApp").build();

try {
    YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet,contentDetails");
    listVideosRequest.setId("A3PDXmYoF5U");
    listVideosRequest.setKey(GoogleAPIKey.DEBUG_GOOGLE_API_KEY);
    VideoListResponse youtubeResponse = listVideosRequest.execute();

    List<Video> youtubeVideos = youtubeResponse.getItems();

    return youtubeVideos;

} catch (IOException e) {
    Log.e("MyApp", e.getLocalizedMessage());
    return null;
}

所以,我总是得到一个例外:403,Access未配置。

我的想法已经不多了,可能是错的。 有人用api键成功地使用了youtube api for android吗?

更新

我只是深入调试了谷歌库,以便找到发送给谷歌的实际请求。就是这样:

https://www.googleapis.com/youtube/v3/videos?id=A3PDXmYoF5U&key=[my-debug-google-api-key]&part=snippet,contentDetails

设置了这些标头:

Accept-Encoding: gzip
User-Agent: MyApp Google-HTTP-Java-Client/1.15.0-rc (gzip)

我没有看到任何错误。

1 个答案:

答案 0 :(得分:0)

好的,经过大量阅读后我发现this google doc

  

您必须为每个插入,更新和删除请求发送授权令牌。您还必须为检索经过身份验证的用户的私有数据的任何请求发送授权令牌。

     

此外,一些用于检索资源的API方法可能支持需要授权的参数,或者在授权请求时可能包含其他元数据。例如,如果请求由该特定用户授权,则检索用户上传的视频的请求也可能包含私有视频

所以结论很简单 - 你应该只使用API​​密钥来读取非私人数据,而视频也可能是私有的。在我的情况下,我试图匿名加载Channels - 当我仅请求channel_id时,它会起作用。当我请求contentDetails(包含上传链接)时 - 我也有403错误。