1

I am able to play videos using the YouTube Player API 1.2.2 for Android. However, users always see pre-roll ads even if they have a YouTube Red subscription. I could not find any documentation surrounding video playback as a specific YouTube user, in order to take advantage of a YouTube Red subscription, which disables ads.

The javadoc for YouTubePlayerSupportFragment.initialize() says:

public void initialize (String developerKey, YouTubePlayer.OnInitializedListener listener)

Initialize a YouTubePlayer, which can be used to play videos and control video playback. One of the callbacks in listener will be invoked when the initialization succeeds or fails.

Parameters

developerKey A valid API key that is enabled to use the YouTube Data API v3 service. To generate a new key, visit the Google Developers Console.
listener The callbacks that will be invoked when the initialization succeeds or fails.

So it takes an API key, rather than a user's OAuth credentials. Is there any way to pass the OAuth credentials instead, to play a video as a specific user?

I saw this example app: https://github.com/youtube/yt-direct-lite-android -- It shows an example of using OAuth 2.0 to upload videos, but the playback uses a simple API key only.

TalkLittle
  • 8,866
  • 6
  • 54
  • 51

2 Answers2

0

There is not currently a way to play videos as a specific user, aside from using WebView. It is the same as the answer to Play private YouTube video using YoutubePlayerView from YoutubeAndroidPlayerAPI?:

This is not possible with the Android Player API. The only way to play private videos on a device is to implement a WebView in your app, have the user log into their YouTube account, and then play back the Private video in that specific browser session only.

TalkLittle
  • 8,866
  • 6
  • 54
  • 51
0

As long as you app uses oath and is verified you can build and play private playlists as if you were the user of the account [for that account]. Using the youtube v3 api and the .player api. If not authenticated properly for whatever reason, you can run into situations where the api lets you create a private playlist as a user of the account but the list is empty within the player api.

also, you can actually play private video:

@SuppressLint("StaticFieldLeak")
private void loadUploadedVideos() {
    if (mChosenAccountName == null) {
        return;
    }

    Log.d(TAG, "loadUploads");

    showProgressDialog();
    new AsyncTask<Void, Void, List<VideoData>>() {
        @Override
        protected List<VideoData> doInBackground(Void... voids) {

            try {

                YouTube youtube = new YouTube.Builder(transport, jsonFactory,
                    credential).setApplicationName(Constants.APP_NAME)
                    .build();


                /*
                 * Now that the user is authenticated, the app makes a
                 * channels list request to get the authenticated user's
                 * channel. Returned with that data is the playlist id for
                 * the uploaded videos.
                 * https://developers.google.com/youtube
                 * /v3/docs/channels/list
                 */
                ChannelListResponse clr = youtube.channels()
                        .list("contentDetails").setMine(true).execute();

                // Get the user's uploads playlist's id from channel list
                // response
                String uploadsPlaylistId = clr.getItems().get(0)
                        .getContentDetails().getRelatedPlaylists()
                        .getUploads();

                List<VideoData> videos = new ArrayList<VideoData>();

                // Get videos from user's upload playlist with a playlist
                // items list request
                PlaylistItemListResponse pilr = youtube.playlistItems()
                        .list("id,contentDetails")
                        .setPlaylistId(uploadsPlaylistId)
                        .setMaxResults(50L).execute();
                List<String> videoIds = new ArrayList<String>();

                // Iterate over playlist item list response to get uploaded
                // videos' ids.
                for (PlaylistItem item : pilr.getItems()) {
                    videoIds.add(item.getContentDetails().getVideoId());
                }

                // Get details of uploaded videos with a videos list
                // request.
                VideoListResponse vlr = youtube.videos()
                        .list("id,snippet,status")
                        .setId(TextUtils.join(",", videoIds)).execute();

                // Add only the public videos to the local videos list.
                for (Video video : vlr.getItems()) {
                    //if ("public".equals(video.getStatus().getPrivacyStatus())) {
                        VideoData videoData = new VideoData();
                        videoData.setVideo(video);
                        videos.add(videoData);
                    //}
                }

                // Sort videos by title
                Collections.sort(videos, new Comparator<VideoData>() {
                    @Override
                    public int compare(VideoData videoData,
                                       VideoData videoData2) {
                        return videoData.getTitle().compareTo(
                                videoData2.getTitle());
                    }
                });

                return videos;

            } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
                showGooglePlayServicesAvailabilityErrorDialog(availabilityException
                        .getConnectionStatusCode());
            } catch (UserRecoverableAuthIOException userRecoverableException) {
                startActivityForResult(
                        userRecoverableException.getIntent(),
                        REQUEST_AUTHORIZATION);
            } catch (IOException e) {
                Utils.logAndShow(MainActivity.this, Constants.APP_NAME, e);
            }
            catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }

            return null;
        }

        @Override
        protected void onPostExecute(List<VideoData> videos) {

            if (videos == null) {
                hideProgressDialog();
                return;
            }

            if (mMyUploadsFragment != null)
                mMyUploadsFragment.setUploads(videos);
            hideProgressDialog();
        }

    }.execute((Void) null);
}

And finally this is the answer to the question, but it needed context:

Also once authenticated, youtube prime features are available in the player api. Nice. The trick seems to be to log the Youtube user into the youtube app on the phone as well as using the same creds in your own app.

anyway any playlist the app is authenticated to play on android can be played this way:

 public void onPlaylistSelected(PlaylistData playlist) {
    
    try {
        mPlaylistData = playlist;
        //Intent intent = YouTubeIntents.createPlayPlaylistIntent(this, playlist.getId());
        Intent intent = YouTubeIntents.createOpenPlaylistIntent(this, playlist.getId());
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Log.w(TAG,e.getMessage());
        Toast.makeText(this, R.string.warn_install_yt,Toast.LENGTH_LONG).show();
    }
}