PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.41k stars 756 forks source link

Add user authentication, to play private videos #845

Open Tusharpalita123 opened 2 years ago

Tusharpalita123 commented 2 years ago

Not Play Youtube Private video

Plz Send Me Suggestions............

PierfrancescoSoffritti commented 2 years ago

There currently is no way to authenticate with the player.

Serkali-sudo commented 2 years ago

@Tusharpalita123 @PierfrancescoSoffritti It is actually possible to authenticate the player. Heres how: Webviews are shared, i mean you can create seperate webviews and acess other webviews login data etc...(youtube iframe api can acess to the account even if you logged in from another webview) So Basically you need to create seperate webview and log into your account there. So first of all set webview url to youtube login page, which is : login link Most important part is when you create that webview you have to enable following settings to keep logged in:

  webView.getSettings().setJavaScriptEnabled(true);
  webView.getSettings().setDomStorageEnabled(true);
  webView.getSettings().setSavePassword(true);
  webView.getSettings().setSaveFormData(true);

After you logged in your youtube account (you dont longer need to show login webview so if you want you can hide it). Then You will able to play private videos. Just play normally it will work (I can make full example if you want)

PierfrancescoSoffritti commented 2 years ago

Hey @Serkali-sudo , thanks for the suggestion.

So you're saying that it's enough to:

  1. create a new webview with these settings
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSavePassword(true);
    webView.getSettings().setSaveFormData(true);
  2. login to YouTube
  3. Create a YouTubePlayerView

and the user should be logged in?

Serkali-sudo commented 2 years ago

Hey @Serkali-sudo , thanks for the suggestion.

So you're saying that it's enough to:

  1. create a new webview with these settings
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSavePassword(true);
    webView.getSettings().setSaveFormData(true);
  2. login to YouTube
  3. Create a YouTubePlayerView

and the user should be logged in?

yes i tested and it works

Tusharpalita123 commented 2 years ago

Can you please provide me the full example of code which you are mentioned.

Serkali-sudo commented 2 years ago

@Tusharpalita123 Heres full example of authenticate the player:

1)First of all Add internet permission

<uses-permission android:name="android.permission.INTERNET"/>

2)Then create main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
        android:id="@+id/youtube_player_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"
        app:autoPlay="false" />

    <Button
        android:id="@+id/log_in"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log in to Youtube"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/play_private"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play Private"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/log_in" />

</androidx.constraintlayout.widget.ConstraintLayout>

3)Create activity_login_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/login_webview"
        android:layout_width="match_parent"
        android:layout_height="400dp" />

</LinearLayout>

4) Create MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.PlayerConstants;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;

public class MainActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private Button log_in_btn, play_private_btn;
    private BottomSheetDialog bottomSheetDialog;
    private YouTubePlayerView youTubePlayerView;
    private final String TAG = MainActivity.class.getSimpleName();
    private YouTubePlayer mYoutubePlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        log_in_btn = findViewById(R.id.log_in);
        youTubePlayerView = findViewById(R.id.youtube_player_view);
        getLifecycle().addObserver(youTubePlayerView);
        youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady(@NonNull YouTubePlayer youTubePlayer) {
                super.onReady(youTubePlayer);
                mYoutubePlayer = youTubePlayer;
            }

            @Override
            public void onError(@NonNull YouTubePlayer youTubePlayer, @NonNull PlayerConstants.PlayerError error) {
                super.onError(youTubePlayer, error);
                if (error == PlayerConstants.PlayerError.VIDEO_NOT_PLAYABLE_IN_EMBEDDED_PLAYER) {
                    //if this happens auth was probably not sucessfull so show log in dialog again
                    log_in();
                }
            }
        });
        play_private_btn = findViewById(R.id.play_private);
        sharedPreferences = getSharedPreferences("log_check", MODE_PRIVATE);

        if (sharedPreferences.getInt("isLogged", -1) == 1) {
            youTubePlayerView.setVisibility(View.VISIBLE);
            log_in_btn.setVisibility(View.GONE);
            play_private_btn.setVisibility(View.VISIBLE);
            Toast.makeText(this, "Logged in", Toast.LENGTH_SHORT).show();

        }
        play_private_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                play_video();
            }
        });

        log_in_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                log_in();
            }
        });

    }

    private void log_in() {
        bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
        View contentView = View.inflate(MainActivity.this, R.layout.activity_login_dialog, null);
        bottomSheetDialog.setContentView(contentView);
        WebView login_web = bottomSheetDialog.findViewById(R.id.login_webview);
        login_web.getSettings().setJavaScriptEnabled(true);
        login_web.getSettings().setDomStorageEnabled(true);
        login_web.getSettings().setSavePassword(true);
        login_web.getSettings().setSaveFormData(true);
        login_web.loadUrl("https://accounts.google.com/ServiceLogin?service=youtube&uilel=3&passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Dm%26hl%3Dtr%26next%3Dhttps%253A%252F%252Fm.youtube.com%252F");
        login_web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //if webview redirects to youtube.com then it means it is logged in
                if (request.getUrl().toString().startsWith("https://m.youtube.com")
                        || request.getUrl().toString().startsWith("https://www.youtube.com")) {
                    Log.d(TAG, "Logged in");
                    sharedPreferences.edit().putInt("isLogged", 1).apply();
                    youTubePlayerView.setVisibility(View.VISIBLE);
                    log_in_btn.setVisibility(View.GONE);
                    play_private_btn.setVisibility(View.VISIBLE);
                    bottomSheetDialog.dismiss();
                    Toast.makeText(MainActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
                    return false;
                }
                return false;
            }
        });

        bottomSheetDialog.show();
    }

    private void play_video() {
        if (mYoutubePlayer != null) {
            //Enter your video id here
            String video_id = "AOHJKPI4bDU";
            mYoutubePlayer.loadVideo(video_id, 0);
        } else {
            Toast.makeText(this, "Player is not initialized yet.Wait a little bit then click again", Toast.LENGTH_LONG).show();
        }

    }

}

And thats it