title: Google_Auth_Exception: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'
body: <p>I just run the example code from <code>https://developers.google.com/api-client-library/php/auth/service-accounts</code> for getting the access token.</p>
  
  <p>But I am getting the following error from my code</p>
  
  <pre><code>Google_Auth_Exception: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' in C:\xampp\htdocs\youtubeapi\Google\Auth\OAuth2.php on line 363
  </code></pre>
  
  <p>My code</p>
  
  <pre><code>&lt;?php
  
  require_once 'Google/autoload.php';
  
  $client_email = 'clinetemailis@7523.com';
  $private_key = file_get_contents('MyProject.p12');
  $scopes = array('https://www.googleapis.com/auth/sqlservice.admin');
  
  $credentials = new Google_Auth_AssertionCredentials(
      $client_email,
      $scopes,
      $private_key
  );
  
  $client = new Google_Client();
  $client-&gt;setAssertionCredentials($credentials);
  if ($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {
    $client-&gt;getAuth()-&gt;refreshTokenWithAssertion();
    echo "dfdsf";
  }
  
  $sqladmin = new Google_Service_SQLAdmin($client);
  $response = $sqladmin-&gt;instances
      -&gt;listInstances('examinable-example-123')-&gt;getItems();
  echo json_encode($response) . "\n";
  ?&gt;
  </code></pre>
  
  <p>please help. whats the actual issue here.</p>
  
  <p>Code Update</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();
  //'2015-08-28T00:00:00.000Z'
  //'2015-08-29T00:00:00.000Z'
  /*
   * 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.
   */
  
  
  $client_id = 'dfgdfgdfg.apps.googleusercontent.com';
      $Email_address = '456dsfd1@developer.gserviceaccount.com';   
      $key_file_location = 'Youtube API-asdasd.p12';      
      $htmlBody="";
      $client = new Google_Client();      
      $client-&gt;setApplicationName("Youtube API");
      $key = file_get_contents($key_file_location);    
  
      // seproate additional scopes with a comma   
      $scopes ="https://www.googleapis.com/auth/analytics.readonly";  
  
      $cred = new Google_Auth_AssertionCredentials($Email_address,         
                               array($scopes),        
                               $key);     
  
      $client-&gt;setAssertionCredentials($cred);
      $youtube = new Google_Service_YouTube($client);
      if($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {        
           $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred);      
      }   
    $_SESSION['token'] = $client-&gt;getAccessToken();
  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']) &amp;&amp; isset($_POST['dateStart']) &amp;&amp; isset($_POST['dateEnd']))
  {
    try {
  
      // Create an object for the liveBroadcast resource's snippet. Specify values
      // for the snippet's title, scheduled start time, and scheduled end time.
      $startTime  = $_POST['dateStart']."T".$_POST['timeStart'].":00+0530";
      $endTime  = $_POST['dateEnd']."T".$_POST['timeEnd'].":00+0530";
      //$startDatetime = new DateTime($startTime);
      //$endDatetime = new DateTime($endTime);
  
      //$startDatetime = $startDatetime-&gt;format(DateTime::ISO8601);
      //$endDatetime = $endDatetime-&gt;format(DateTime::ISO8601);
      $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
      $broadcastSnippet-&gt;setTitle($_POST['title']);
      $broadcastSnippet-&gt;setScheduledStartTime($startTime);
      $broadcastSnippet-&gt;setScheduledEndTime($endTime);
  
      // 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 {
  
  
  }
  } 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>
  
  <p>I am using this authentication for scheduling the live stream once i using this code. i am getting another error <code>Error calling POST https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet%2Cstatus: (403) Insufficient Permission</code></p>
  
  <p>How can i make my request with sufficient permission?</p>