Reputation: 131
I'm trying to use Google Service Account to list videos from YouTube Data API v3. I activated YouTube API, created Account Service, but whenever I try to list videos from my channel, it says 0 videos even if I do have uploaded videos.
I ran samples from their docs but it seems like my credentials list videos from my service account directly and not my real YouTube account.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("youtube-****serviceaccount.com")
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(new File("C:\\tmp\\youtube.p12"))
.build();
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
"youtube-cmdline-uploadvideo-sample").build();
YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
channelRequest.setMine(true);
ChannelListResponse channelResult = channelRequest.execute();
List<Channel> channelsList = channelResult.getItems();
String uploadPlaylistId = channelsList.get(0).getContentDetails().getRelatedPlaylists().getUploads();
uploadPlaylistId
should be here my channel ID, but I do not know what it is, all I know it's that this is an empty channel. (If i check here using my real ID channel, I can list my uploads, it seems my service account does not refer correctly on my YouTube account...)
Upvotes: 1
Views: 1402
Reputation: 116948
The problem you are having is that you are trying to use a service account with the YouTube API. Service accounts are by default dummy users with there own accounts. The service account does not have anything uploaded to its account and I don't think you can upload anything to it.
Normally with the Google APIs you would grant the service account access to your account by adding it as a user or granting it permissions some how. Unfortunately this is not possible with the YouTube API.
if you try and use a service account with the YouTube api you will normally see this error.
unauthorized (401) youtubeSignupRequired
This error is commonly seen if you try to use the OAuth 2.0 Service Account flow. YouTube does not support Service Accounts, and if you attempt to authenticate using a Service Account, you will get this error.
The above information can be found in the YouTube Data API Error documentation here
You will need to switch to using Oauth2
Upvotes: 1