com.google.api.services.youtube.YouTube クラスを使用して、Java アプリから YouTube チャンネルのリストを取得しようとしています。.
まず、サービス アカウントの資格情報 ( https://console.developers.google.com > Credentials ) を有効にし、次の API を有効にしました: -YouTube Analytics API -YouTube Data API -Analytics API
Youtube サービスを呼び出すために、次のコードを使用して Credential オブジェクトを作成します。
/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static Credential authorize() throws Exception
{
List<String> scopes = new ArrayList<String>();
scopes.add("https://www.googleapis.com/auth/youtube");
scopes.add("https://www.googleapis.com/auth/yt-analytics.readonly");
scopes.add("https://www.googleapis.com/auth/youtube.readonly");
scopes.add("https://www.googleapis.com/auth/youtubepartner-channel-audit");
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountPrivateKeyFromP12File(new File("C://file.privatekey.p12"))
.setServiceAccountId("xxxx-yyyyyyyyyyyyyy@developer.gserviceaccount.com")
.setServiceAccountScopes(scopes)
.build();
return credential;
}
その後、YouTube サービスを呼び出してチャンネルを取得します
/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
/** Global instance of Youtube object to make general YouTube API requests. */
private static YouTube youtube;
/** Global instance of YoutubeAnalytics object to make analytic API requests. */
private static YouTubeAnalytics analytics;
public String getDefaultChannelId(){
try{
Credential credential = authorize();
// YouTube object used to make all non-analytic API requests.
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("API Project")
.build();
YouTube.Channels.List channelRequest = youtube.channels().list("id,snippet");
channelRequest.setMine(true);
channelRequest.setMaxResults(50L);
channelRequest.setFields("items(id,snippet/title,contentDetails,status,kind,etag,auditDetails)");
ChannelListResponse channels = channelRequest.execute();
System.out.println(channels.getItems());
// List of channels associated with user.
List<Channel> listOfChannels = channels.getItems();
// Grab default channel which is always the first item in the list.
Channel defaultChannel = listOfChannels.get(0);
String channelId = defaultChannel.getId();
return channelId;
}catch(Exception ex){
ex.printStacktrace();
}
}
認証コードは問題なく動作しているようです。問題は、ID UC9i22sTxrX0IQk4AkT_Og3w のチャネルを返す getDefaultChannelId() メソッドにあります。ブラウザーを使用して、次の URL を使用して YouTube チャンネルに移動しようとしました: http://www.youtube.com/channel/UC9i22sTxrX0IQk4AkT_Og3w しかし、チャンネルが存在しません..
チャネルの結果を出力するために使用した行は、「System.out.println(channels.getItems());」です。次の json 文字列を表示します。
[{"etag":"\"BDC7VThyM9nfoSQm1_kOyhtJTEw/yJvLzly7DMctrvFV5drOtgksadM\"","id":"UC9i22sTxrX0IQk4AkT_Og3w","kind":"youtube#channel","snippet":{"title":""}}
何らかの理由で、YouTube サービスが特定の資格情報オブジェクトのチャンネルの正しいリストを返しません。しかし、なぜ????