According to Google documentation, any Google account holder is also a Youtube account holder, but he may not be "linked", i.e. have a Youtube channel. An app can upload video only to a linked account.
If you use AccountManager to get a list of Google account holders, at that point, you can't tell whether any one of them is linked or not. I need a way to find out if it's linked.
There does not seem to be a direct purpose-built call to the Youtube api to see if an account has a channel (is linked).
Maybe the following snippet of the code could be re-purposed to do it:
List<String> scopes = new ArrayList<>();
scopes.add( SendToGoogleUtils."https://www.googleapis.com/auth/youtube.upload" );
if( ( googleAccountCredential = SendToGoogleUtils.getGoogleAccountCredential(
mContext, accountYoutube.name, scopes ) ) == null) return;
String gAToken = googleAccountCredential.getToken();
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, googleAccountCredential)
.setApplicationName(getString(R.string.tourmaker_app_name))
.build();
YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
channelRequest.setMine(true);
channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
ChannelListResponse channelResult = channelRequest.execute();
If accountYoutube (see code line 4) is linked, this code runs ok, and proceeds to later code that uploads.
If accountYoutube is not linked, the execute statement throws the GoogleJsonResponseException:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code" : 403, "errors" : [ { "domain" : "global", "message" : "Insufficient Permission", "reason" : "insufficientPermissions" } ], "message" : "Insufficient Permission" }
So, this code kind of tells you if the accountYoutube is linked. Would this sort of code be a reasonable way to determine if accountYoutube has a channel (is linked)?
I'm suspicious, because this exception seems a side-effect I may not be able to rely on.
I'm looking for a best-practices way to determine if accounYoutube is linked. I would use the code at an appropriate time to present to User only linked accounts.
Does anyone know a reliable way to do it?
Based on this SO question, the error 403/InsufficientPermissions is returned when you have not requested the scopes that you need for your application.
While the description of insufficientPermissions
in the Google documentation is that the OAuth 2.0 token provided for the request specifies scopes that are insufficient for accessing the requested data.
So make sure you request all of the scopes that you need.
You can use the channels that have a properties status.isLinked
that will return a boolean value that will indicate whether the channel data identifies a user that is already linked to either a YouTube username or a Google+ account. A user that has one of these links already has a public YouTube identity, which is a prerequisite for several actions, such as uploading videos.
For more information you can also check this SO question if it can help you.