How to see if Google account holder has a Youtube channel (is linked)?

时间:2016-06-18 20:20:35

标签: android youtube-api google-account

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?

1 个答案:

答案 0 :(得分:0)

对于错误404或不足的权限。

基于此SO question,如果您未请求应用程序所需的范围,则会返回错误403 / InsufficientPermissions。

虽然Google文档中insufficientPermissions的说明是为请求提供的OAuth 2.0令牌指定的范围不足以访问请求的数据。

因此,请确保您请求所需的所有范围。

有关确定YouTube频道是否与帐户相关联的问题。

您可以使用具有属性status.isLinked的频道,该频道将返回一个布尔值,该值将指示频道数据是否标识已链接到YouTube用户名或Google+帐户的用户。拥有其中一个链接的用户已拥有公开的YouTube身份,这是多个操作的先决条件,例如上传视频。

有关详细信息,您还可以查看此SO question是否可以为您提供帮助。