2014-09-30 135 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