title: PHP How to upload to Youtube user's channel
body: <p>Need to make user's authorizations so server will upload videos to users channels.</p>
  
  <p>Authentication part:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setAuthConfigFile(PROPPATH.'/inc/client_secret.json');
  $client-&gt;setRedirectUri('https://back url');
  $client-&gt;setScopes('https://www.googleapis.com/auth/youtube');
  $client-&gt;setAccessType('offline');
  $credentialsPath = PROPPATH.'/youtube_auth/user_'.get_current_user_id().'.json';
  if (file_exists($credentialsPath)) {
      $accessToken = file_get_contents($credentialsPath);
  } else {
  
  $authUrl = $client-&gt;createAuthUrl();
  if(!isset($_GET['code'])) {
      echo '&lt;script&gt;window.location = "'.$authUrl.'";&lt;/script&gt;';
  } else {
      $authCode = $_GET['code'];
      $accessToken = $client-&gt;authenticate($authCode);
      file_put_contents($credentialsPath, $accessToken);
  }
  </code></pre>
  
  <p>Authentication seems like works and saves some key.
  2nd part, trying upload video:</p>
  
  <pre><code>$client = new Google_Client();
  $client-&gt;setAuthConfigFile(PROPPATH.'/inc/client_secret.json');
  $client-&gt;setScopes('https://www.googleapis.com/auth/youtube');
  $youtube = new Google_Service_YouTube($client);
  $client-&gt;setAccessToken(file_get_contents(PROPPATH.'/youtube_auth/user_2.json'));
  $client-&gt;setAccessType('offline');
  if ($client-&gt;getAccessToken()) {
  $video = new Google_Service_YouTube_Video();
  $chunkSizeBytes = 1 * 1024 * 1024;
  $client-&gt;setDefer(true);
  $insertRequest = $youtube-&gt;videos-&gt;insert("", $video);
  $media = new Google_Http_MediaFileUpload(
      $client,
      $insertRequest,
      'video/*',
      null,
      true,
      $chunkSizeBytes
  );
  $media-&gt;setFileSize(filesize($videoPath));
  ...
  </code></pre>
  
  <p>And i'm getting error:</p>
  
  <blockquote>
    <p>A service error occurred: { "error": { "errors": [ { "domain":
    "global", "reason": "authError", "message": "Invalid Credentials",
    "locationType": "header", "location": "Authorization" } ], "code":
    401, "message": "Invalid Credentials" } }
    What i'm missing?</p>
  </blockquote>