title: Google account auto Login and get access token using php
body: <p>I am using You tube API for live streaming in my application. For scheduling the live streaming I will give the one common email id and password for every users. </p>
  
  <p>I need to login the user into google account automatically( Without giving the email id and password) using the common email id and password and get access token using php.</p>
  
  <p>Is it possible?</p>
  
  <p>My current code is below.please suggest one solution!!</p>
  
  <pre><code>&lt;?php
  
  // Call set_include_path() as needed to point to your client library.
  require_once 'Google/autoload.php';
  require_once 'Google/Client.php';
  require_once 'Google/Service/YouTube.php';
  session_start();
  
  /*
   * You can acquire an OAuth 2.0 client ID and client secret from the
   * Google Developers Console &lt;https://console.developers.google.com/&gt;
   * For more information about using OAuth 2.0 to access Google APIs, please see:
   * &lt;https://developers.google.com/youtube/v3/guides/authentication&gt;
   * Please ensure that you have enabled the YouTube Data API for your project.
   */
  $OAUTH2_CLIENT_ID = '$OAUTH2_CLIENT_ID';
  $OAUTH2_CLIENT_SECRET = '$OAUTH2_CLIENT_SECRET';
  $htmlBody="";
  $client = new Google_Client();
  $client-&gt;setClientId($OAUTH2_CLIENT_ID);
  $client-&gt;setClientSecret($OAUTH2_CLIENT_SECRET);
  $client-&gt;setScopes('https://www.googleapis.com/auth/youtube');
  $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
      FILTER_SANITIZE_URL);
  $client-&gt;setRedirectUri($redirect);
  
  // Define an object that will be used to make all API requests.
  $youtube = new Google_Service_YouTube($client);
  
  if (isset($_GET['code'])) {
    if (strval($_SESSION['state']) !== strval($_GET['state'])) {
      die('The session state did not match.');
    }
  
    $client-&gt;authenticate($_GET['code']);
    $_SESSION['token'] = $client-&gt;getAccessToken();
    header('Location: ' . $redirect);
  }
  
  if (isset($_SESSION['token'])) {
    $client-&gt;setAccessToken($_SESSION['token']);
  }
  
  // Check to ensure that the access token was successfully acquired.
  if ($client-&gt;getAccessToken()) {
  if(isset($_POST['title']) &amp;&amp; isset($_POST['timeStart']) &amp;&amp; isset($_POST['timeEnd']) &amp;&amp; isset($_POST['Status']))
  {
    try {
      // Create an object for the liveBroadcast resource's snippet. Specify values
      // for the snippet's title, scheduled start time, and scheduled end time.
      $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
      $broadcastSnippet-&gt;setTitle($_POST['title']);
      $broadcastSnippet-&gt;setScheduledStartTime($_POST['timeStart']);
      $broadcastSnippet-&gt;setScheduledEndTime($_POST['timeEnd']);
  
      // Create an object for the liveBroadcast resource's status, and set the
      // broadcast's status to "private".
      $status = new Google_Service_YouTube_LiveBroadcastStatus();
      $status-&gt;setPrivacyStatus($_POST['Status']);
  
      // Create the API request that inserts the liveBroadcast resource.
      $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
      $broadcastInsert-&gt;setSnippet($broadcastSnippet);
      $broadcastInsert-&gt;setStatus($status);
      $broadcastInsert-&gt;setKind('youtube#liveBroadcast');
  
      // Execute the request and return an object that contains information
      // about the new broadcast.
      $broadcastsResponse = $youtube-&gt;liveBroadcasts-&gt;insert('snippet,status',
          $broadcastInsert, array());
  
      // Create an object for the liveStream resource's snippet. Specify a value
      // for the snippet's title.
      $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
      $streamSnippet-&gt;setTitle('New Stream');
  
      // Create an object for content distribution network details for the live
      // stream and specify the stream's format and ingestion type.
      $cdn = new Google_Service_YouTube_CdnSettings();
      $cdn-&gt;setFormat("1080p");
      $cdn-&gt;setIngestionType('rtmp');
  
      // Create the API request that inserts the liveStream resource.
      $streamInsert = new Google_Service_YouTube_LiveStream();
      $streamInsert-&gt;setSnippet($streamSnippet);
      $streamInsert-&gt;setCdn($cdn);
      $streamInsert-&gt;setKind('youtube#liveStream');
  
      // Execute the request and return an object that contains information
      // about the new stream.
      $streamsResponse = $youtube-&gt;liveStreams-&gt;insert('snippet,cdn',
          $streamInsert, array());
  
      // Bind the broadcast to the live stream.
      $bindBroadcastResponse = $youtube-&gt;liveBroadcasts-&gt;bind(
          $broadcastsResponse['id'],'id,contentDetails',
          array(
              'streamId' =&gt; $streamsResponse['id'],
          ));
  
      $htmlBody .= "&lt;h3&gt;Added Broadcast&lt;/h3&gt;&lt;ul&gt;";
      $htmlBody .= sprintf('&lt;li&gt;%s published at %s (%s)&lt;/li&gt;',
          $broadcastsResponse['snippet']['title'],
          $broadcastsResponse['snippet']['publishedAt'],
          $broadcastsResponse['id']);
      $htmlBody .= '&lt;/ul&gt;';
      $htmlBody .= "&lt;h3&gt;Added Stream&lt;/h3&gt;&lt;ul&gt;";
      $htmlBody .= sprintf('&lt;li&gt;%s (%s)&lt;/li&gt;',
          $streamsResponse['snippet']['title'],
          $streamsResponse['id']);
      $htmlBody .= '&lt;/ul&gt;';
      $htmlBody .= "&lt;h3&gt;Bound Broadcast&lt;/h3&gt;&lt;ul&gt;";
      $htmlBody .= sprintf('&lt;li&gt;Broadcast (%s) was bound to stream (%s).&lt;/li&gt;',
          $bindBroadcastResponse['id'],
          $bindBroadcastResponse['contentDetails']['boundStreamId']);
      $htmlBody .= '&lt;/ul&gt;';
      //$htmlBody .="&lt;h3&gt;Live  Broadcast&lt;/h3&gt;&lt;div&gt;";
  
      //$htmlBody .= sprintf("&lt;iframe id='ytplayer' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/%s?autoplay=1'  frameborder='0'&gt;&lt;/iframe&gt;",
          //  $broadcastsResponse['id']);
      $htmlBody .= '&lt;/div&gt;';
  
  
    } catch (Google_Service_Exception $e) {
      $htmlBody .= sprintf('&lt;p&gt;A service error occurred: &lt;code&gt;%s&lt;/code&gt;&lt;/p&gt;',
          htmlspecialchars($e-&gt;getMessage()));
        // echo $e-&gt;getMessage();
    } catch (Google_Exception $e) {
      $htmlBody .= sprintf('&lt;p&gt;An client error occurred: &lt;code&gt;%s&lt;/code&gt;&lt;/p&gt;',
          htmlspecialchars($e-&gt;getMessage()));
    }
  
    $_SESSION['token'] = $client-&gt;getAccessToken();
    } else {
    if(isset($_GET['propid']))
    {
    $htmlBody = &lt;&lt;&lt;END
    &lt;form action="" method="post"&gt;
  &lt;input type="text" name="title" value="Event Title"/&gt;&lt;br/&gt;
  &lt;input type="text" name="timeStart" value="Start Time"/&gt;&lt;br/&gt;
  &lt;input type="text" name="timeEnd" value="End Time"/&gt;&lt;br/&gt;
  &lt;input type="text" name="Status" value="Public,Unlisted,Private"/&gt;
  &lt;input type ="submit" value="Submit"/&gt;
  &lt;/form&gt;
  END;
  } else {
  
   $htmlBody = &lt;&lt;&lt;END
   &lt;p&gt;Close this window and click the schedule button for live streaming&lt;/p&gt;
  &lt;p&gt;You need to &lt;a href="#"  onclick="window.close();"&gt;Close Window&lt;/a&gt; before proceeding.&lt;p&gt;
  END;
  
  }
  
  }
  } else {
    // If the user hasn't authorized the app, initiate the OAuth flow
    $state = mt_rand();
    $client-&gt;setState($state);
    $_SESSION['state'] = $state;
  
    $authUrl = $client-&gt;createAuthUrl();
    $htmlBody = &lt;&lt;&lt;END
    &lt;h3&gt;Authorization Required&lt;/h3&gt;
    &lt;p&gt;You need to &lt;a href="$authUrl" onclick="window.open('$authUrl', 'newwindow', 'width=600, height=400'); return false;"&gt;authorize access&lt;/a&gt; before proceeding.&lt;p&gt;
  END;
  }
  ?&gt;
  
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Bound Live Broadcast&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;?=$htmlBody?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>