.NET Code Samples
Stay organized with collections
Save and categorize content based on your preferences.
The following code samples, which use the Google APIs Client Library for .NET
, are available for the YouTube Content ID API
.
Retrieve a content owner's managed channels
The following code sample calls the YouTube Data API's channels.list
method to retrieve a list of channels managed by the content owner making the API request.
using
System
;
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
System
.
Reflection
;
/*
*
External
dependencies
,
OAuth
2.0
support
,
and
core
client
libraries
are
at
:
*
https
:
//
developers
.
google
.
com
/
api
-
client
-
library
/
dotnet
/
apis
/
*
Also
see
the
Samples
.
zip
file
for
the
Google
.
Apis
.
Samples
.
Helper
classes
at
:
*
https
:
//
github
.
com
/
youtube
/
api
-
samples
/
tree
/
master
/
dotnet
*/
using
DotNetOpenAuth
.
OAuth2
;
using
Google
.
Apis
.
Authentication
;
using
Google
.
Apis
.
Authentication
.
OAuth2
;
using
Google
.
Apis
.
Authentication
.
OAuth2
.
DotNetOpenAuth
;
using
Google
.
Apis
.
Samples
.
Helper
;
using
Google
.
Apis
.
Services
;
using
Google
.
Apis
.
Util
;
using
Google
.
Apis
.
Youtube
.
v3
;
using
Google
.
Apis
.
Youtube
.
v3
.
Data
;
namespace
dotnet
{
class
papi_my_managed_channels
{
static
void
Main
(
string
[]
args
)
{
CommandLine
.
EnableExceptionHandling
();
CommandLine
.
DisplayGoogleSampleHeader
(
"YouTube Partner API: My Managed Channels"
);
var
credentials
=
PromptingClientCredentials
.
EnsureFullClientCredentials
();
var
provider
=
new
NativeApplicationClient
(
GoogleAuthenticationServer
.
Description
)
{
ClientIdentifier
=
credentials
.
ClientId
,
ClientSecret
=
credentials
.
ClientSecret
};
var
auth
=
new
OAuth2Authenticator<NativeApplicationClient>
(
provider
,
GetAuthorization
);
var
youtube
=
new
YoutubeService
(
new
BaseClientService
.
Initializer
()
{
Authenticator
=
auth
});
var
contentOwnerId
=
CommandLine
.
RequestUserInput<string>
(
"Content Owner ID"
);
var
nextPageToken
=
""
;
while
(
nextPageToken
!=
null
)
{
var
channelsListRequest
=
youtube
.
Channels
.
List
(
"snippet"
);
channelsListRequest
.
MaxResults
=
50
;
channelsListRequest
.
ManagedByMe
=
true
;
channelsListRequest
.
OnBehalfOfContentOwner
=
contentOwnerId
;
channelsListRequest
.
PageToken
=
nextPageToken
;
var
channelsListResponse
=
channelsListRequest
.
Fetch
();
foreach
(
var
channelItem
in
channelsListResponse
.
Items
)
{
CommandLine
.
WriteLine
(
String
.
Format
(
"{0} ({1})"
,
channelItem
.
Snippet
.
Title
,
channelItem
.
Id
));
}
nextPageToken
=
channelsListResponse
.
NextPageToken
;
}
CommandLine
.
PressAnyKeyToExit
();
}
private
static
IAuthorizationState
GetAuthorization
(
NativeApplicationClient
client
)
{
var
storage
=
MethodBase
.
GetCurrentMethod
()
.
DeclaringType
.
ToString
();
var
key
=
"storage_key"
;
IAuthorizationState
state
=
AuthorizationMgr
.
GetCachedRefreshToken
(
storage
,
key
);
if
(
state
!=
null
)
{
client
.
RefreshToken
(
state
);
}
else
{
state
=
AuthorizationMgr
.
RequestNativeAuthorization
(
client
,
YoutubeService
.
Scopes
.
YoutubeReadonly
.
GetStringValue
(),
YoutubeService
.
Scopes
.
Youtubepartner
.
GetStringValue
());
AuthorizationMgr
.
SetCachedRefreshToken
(
storage
,
key
,
state
);
}
return
state
;
}
}
}
All rights reserved. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eThis content provides a code sample utilizing the Google APIs Client Library for .NET to interact with the YouTube Content ID API.\u003c/p\u003e\n"],["\u003cp\u003eThe code demonstrates how to retrieve a list of channels that are managed by a specific content owner through the \u003ccode\u003echannels.list\u003c/code\u003e method of the YouTube Data API.\u003c/p\u003e\n"],["\u003cp\u003eThe sample requires OAuth 2.0 authentication, using \u003ccode\u003eNativeApplicationClient\u003c/code\u003e to handle credentials and authorization, with specific scopes including \u003ccode\u003eYoutubeReadonly\u003c/code\u003e and \u003ccode\u003eYoutubepartner\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe code fetches and displays the titles and IDs of up to 50 channels per page, with support for pagination using the \u003ccode\u003enextPageToken\u003c/code\u003e to retrieve additional results.\u003c/p\u003e\n"]]],["This code utilizes the YouTube Data API's `channels.list` method to retrieve channels managed by a content owner. It uses the Google APIs Client Library for .NET and OAuth 2.0 for authentication. The script requests a \"Content Owner ID\", then iterates through paginated results, fetching up to 50 channels per request using the `ManagedByMe` filter. For each channel found, the title and ID are displayed. The code handles OAuth 2.0 authentication, including caching and refreshing tokens.\n"],null,["# .NET Code Samples\n\nThe following code samples, which use the [Google APIs Client Library for .NET](/api-client-library/dotnet), are available for the YouTube Content ID API.\n\nRetrieve a content owner's managed channels\n-------------------------------------------\n\nThe following code sample calls the YouTube Data API's `channels.list` method to retrieve a list of channels managed by the content owner making the API request. \n\n```gdscript\nusing System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing System.Reflection;\n\n/*\n * External dependencies, OAuth 2.0 support, and core client libraries are at:\n * https://developers.google.com/api-client-library/dotnet/apis/\n * Also see the Samples.zip file for the Google.Apis.Samples.Helper classes at:\n * https://github.com/youtube/api-samples/tree/master/dotnet\n */\n\nusing DotNetOpenAuth.OAuth2;\n\nusing Google.Apis.Authentication;\nusing Google.Apis.Authentication.OAuth2;\nusing Google.Apis.Authentication.OAuth2.DotNetOpenAuth;\nusing Google.Apis.Samples.Helper;\nusing Google.Apis.Services;\nusing Google.Apis.Util;\nusing Google.Apis.Youtube.v3;\nusing Google.Apis.Youtube.v3.Data;\n\nnamespace dotnet\n{\n class papi_my_managed_channels\n {\n static void Main(string[] args)\n {\n CommandLine.EnableExceptionHandling();\n CommandLine.DisplayGoogleSampleHeader(\"YouTube Partner API: My Managed Channels\");\n\n var credentials = PromptingClientCredentials.EnsureFullClientCredentials();\n var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)\n {\n ClientIdentifier = credentials.ClientId,\n ClientSecret = credentials.ClientSecret\n };\n var auth = new OAuth2Authenticator\u003cNativeApplicationClient\u003e(provider, GetAuthorization);\n\n var youtube = new YoutubeService(new BaseClientService.Initializer()\n {\n Authenticator = auth\n });\n\n var contentOwnerId = CommandLine.RequestUserInput\u003cstring\u003e(\"Content Owner ID\");\n\n var nextPageToken = \"\";\n while (nextPageToken != null)\n {\n var channelsListRequest = youtube.Channels.List(\"snippet\");\n channelsListRequest.MaxResults = 50;\n channelsListRequest.ManagedByMe = true;\n channelsListRequest.OnBehalfOfContentOwner = contentOwnerId;\n channelsListRequest.PageToken = nextPageToken;\n\n var channelsListResponse = channelsListRequest.Fetch();\n\n foreach (var channelItem in channelsListResponse.Items)\n {\n CommandLine.WriteLine(String.Format(\"{0} ({1})\", channelItem.Snippet.Title, channelItem.Id));\n }\n\n nextPageToken = channelsListResponse.NextPageToken;\n }\n \n CommandLine.PressAnyKeyToExit();\n }\n\n private static IAuthorizationState GetAuthorization(NativeApplicationClient client)\n {\n var storage = MethodBase.GetCurrentMethod().DeclaringType.ToString();\n var key = \"storage_key\";\n\n IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(storage, key);\n if (state != null)\n {\n client.RefreshToken(state);\n }\n else\n {\n state = AuthorizationMgr.RequestNativeAuthorization(client, YoutubeService.Scopes.YoutubeReadonly.GetStringValue(), YoutubeService.Scopes.Youtubepartner.GetStringValue());\n AuthorizationMgr.SetCachedRefreshToken(storage, key, state);\n }\n\n return state;\n }\n }\n}\n```"]]