Thursday, March 9, 2017

Issue with liveBroadcasts.insert and liveStreams.insert methods to create a broadcast and a stream using Youtube API

I'm trying to use Youtube API php code example to create broadcast and stream. https://developers.google.com/youtube/v3/live/docs/liveBroadcasts/insert But actually I have oAuth problem. As I'm using Laravel 5.4 I changed the code a little (the pure didn't work as well). $_SESSION to session()... I used redirect instead of header('Location:', ... ).

May be I don't understand it properly, but I'm confused about how it should redirect if the 'code' never exists in $request before redirection.

    public function create(Request $request)
        {

            $OAUTH2_CLIENT_ID = env('OAUTH2_CLIENT_ID');
            $OAUTH2_CLIENT_SECRET = env('OAUTH2_CLIENT_SECRET');

            $client = new Google_Client();
            $client->setClientId($OAUTH2_CLIENT_ID);
            $client->setClientSecret($OAUTH2_CLIENT_SECRET);
            $client->setScopes('https://www.googleapis.com/auth/youtube');
            $client->setDeveloperKey('*my dev key*');
            $redirect = url('/broadcast/create');
            $client->setRedirectUri($redirect);


            $youtube = new Google_Service_YouTube($client);

            $tokenSessionKey = 'token_auth_youtube';

            if ($request->code) {
                var_dump(session()->get('state'));
                var_dump($request->state);
                if (strval(session()->get('state')) !== strval($request->state)) {
                    die('The session state did not match.');
                }

                $client->authenticate($request->code);
                session([$tokenSessionKey => $client->getAccessToken()]);
                return redirect($redirect);
            }
            if (session()->get($tokenSessionKey)) {
                $client->setAccessToken(session()->get($tokenSessionKey));
            }

            $htmlBody='';

            if ($client->getAccessToken()) {

           **//my goal is to reach here**
                ..........
             //a lot of code

                session([$tokenSessionKey => $client->getAccessToken()]);
            } elseif ($OAUTH2_CLIENT_ID == env('OAUTH2_CLIENT_ID')) {
                $htmlBody = <<<END
          <h3>Client Credentials Required</h3>
          <p>
            You need to set <code>\$OAUTH2_CLIENT_ID</code> and
            <code>\$OAUTH2_CLIENT_ID</code> before proceeding.
          <p>
    END;
            } else {
                // If the user hasn't authorized the app, initiate the OAuth flow
                $state = mt_rand();
                $client->setState($state);
                session(['state' => $state]);

                $authUrl = $client->createAuthUrl();
                $htmlBody = <<<END
          <h3>Authorization Required</h3>
          <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
    END;
            }
            return view('welcome', compact('htmlBody'));
        }



via Vlad Sarukhanyan

Advertisement