2014-09-30 54 views
2

我想在Android应用检索YouTube频道,并具有下面的代码:如何在Youtube的Android API中将API密钥放入Youtube.Builder中?

HttpTransport transport = new NetHttpTransport(); 
JsonFactory jsonFactory = new GsonFactory(); 
GoogleCredential credential = new GoogleCredential() 
     .setAccessToken("<my api key>"); 
YouTube yt = new YouTube.Builder(transport, jsonFactory, 
     credential).build(); 
try { 
    List l = yt.channels().list("id"); 
    l.put("id", "UCO3bfz4KY6zGT5IOaFJuxpA"); 
    ChannelListResponse resp = l.execute(); 
    java.util.List<Channel> chs = resp.getItems(); 
    for (Channel ch : chs) { 
     // ... 
    } 
} catch (IOException e1) { 
    e1.printStackTrace(); 
} 

但我得到的错误响应消息"Invalid Credentials"。我正在使用https://console.developers.google.com/project/<project name>/apiui/credential中的API密钥,分段为Public API access,API KEY

我不知道我使用了错误的键,错误的API,设置错误,不应该使用调试键或其他错误运行。谁能帮忙?谢谢!

回答

1

我发现该方法在List

List l = yt.channels().list("id"); 
l.setKey("<Browser API Key"); // This line 
l.put("id", "UCO3bfz4KY6zGT5IOaFJuxpA"); 

请注意,请勿在此处使用Android API密钥,因为它适用于Player API,而不是Data API。我需要创建一个带有空引用限制的新浏览器键,以使其工作。

使用setKey()查看谷歌的YouTube>数据API的网站上完整的Java代码,例如:
https://developers.google.com/youtube/v3/docs/videos/list#examples