安卓 - 优酷 API r52 - "access not configured"



我正在构建一个简单的测试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 not configured.

我已经想不出什么主意了,会出什么问题呢?有人已经成功地使用了android的youtube api与api密钥吗?

更新:

我刚刚深入调试了google库,以便找到发送给google的实际请求是什么样子的。它是:

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)

我看不出有什么问题

好了,在大量阅读之后,我找到了这个google文档:

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

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

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

最新更新