title: Google Analytics API 401 Invalid Credentials
body: <p>I'm wracking my brain trying to figure out how to use the <a href="https://code.google.com/p/google-api-php-client/source/browse/trunk/src/contrib/Google_AnalyticsService.php" rel="nofollow">Google API PHP Client</a> to access Google Analytics. Specifically, I want to <a href="https://developers.google.com/analytics/devguides/platform/features/cost-data-overview" rel="nofollow">upload cost data</a> from other campaigns. My code does work to get information from GA, I can view traffic data for profiles, but I cannot figure out how to upload.</p>
  
  <p>Here's the code that I'm using for auth:</p>
  
  <pre><code>require_once 'google-api-php-client/src/Google_Client.php';
  require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
  session_start();
  
  $client = new Google_Client();
  $client-&gt;setApplicationName("GA-Tester");
  $client-&gt;setClientId('xxx.apps.googleusercontent.com');
  $client-&gt;setClientSecret('xxx');
  $client-&gt;setRedirectUri('http://www.site.com/indext.php');
  $client-&gt;setDeveloperKey('xxx');
  
  $analyticsService = new Google_AnalyticsService($client);
  $dailyUploads = $analyticsService-&gt;management_dailyUploads;
  
  if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
  }
  if (isset($_GET['code'])) {
    $client-&gt;authenticate();
    $_SESSION['token'] = $client-&gt;getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  if (isset($_SESSION['token'])) {
    $client-&gt;setAccessToken($_SESSION['token']);
  }
  if (!$client-&gt;getAccessToken()) {
    header ('Location:http://www.site.com/indext.php');
  } else {...
  </code></pre>
  
  <p>This code does work for requesting data. I can get a list of accounts, profiles, download traffic data for a specific profile, etc. No errors or issues.</p>
  
  <p>When I try to upload my CSV file containing cost data I get a '401 Invalid Credentials' error. Here's the code that sends the file:</p>
  
  <pre><code>$send = $dailyUploads-&gt;upload(
    $accountId,
    $webPropertyId,
    $customDataSourceId,
    $start_date,
    1, 
   'cost',
    array(
     "reset" =&gt; true, 
     "data" =&gt; $cont,
     "mimeType" =&gt; "application/octet-stream", 
     "uploadType" =&gt; "media"));
  </code></pre>
  
  <p>I've double checked all of my variables that I'm passing, they're sending the correct information.</p>
  
  <p>So here's my question. If all of my <code>GET</code> requests work without issue, why would my <code>POST</code> request throw an error? I can't tell if this is an error with my code, or with settings in the <a href="https://code.google.com/apis/console/" rel="nofollow">API console</a>. Can anyone steer me in the right direction?</p>
  
  <p><strong>EDIT:</strong>
  Here's the error code generated (minus identifiable bits).</p>
  
  <pre><code>Fatal error: Uncaught exception 'Google_ServiceException' with message 
  'Error calling POST https://www.googleapis.com/upload/analytics/v3/man
  agement/accounts/1234567/webproperties/UA-1234567-1/customDataSources/
  xXxXxX/dailyUploads/2013-01-17/uploads?appendNumber=1&amp;type=cost&amp;reset=
  true&amp;uploadType=media&amp;key=xXxXxX: (401) Invalid Credentials' in /../pub
  lic_html/proj/google-api-php-client/src/io/Google_REST.php:66 Stack tra
  ce: #0 /../public_html/proj/google-api-php-client/src/io/Google_REST.ph
  p(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /
  ../public_html/proj/google-api-php-client/src/service/Google_ServiceRes
  ource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /..
  /public_html/proj/google-api-php-client/src/contrib/Google_AnalyticsSer
  vice.php(82): Google_ServiceResource-&gt;__call('upload', Array) #3 /../pu
  blic_html/proj/dailyUpload_send.php(60): Google_ManagementDailyUploadsS
  erviceResource-&gt;upload('1234567', 'UA-1234567-1', 'xXxXxXxXxXxXxXx...',
   ' in /../public_html/proj/google-api-php-client/src/io/Google_REST.php
    on line 66
  </code></pre>