title: Google Analytics OAuth2: How to solve error: "redirect_uri_mismatch"?
body: <p>I'm trying to get this example to work: <a href="https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php#enable" rel="nofollow noreferrer">https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php#enable</a></p>
  
  <p>The error I'm getting is "Error: redirect_uri_mismatch" .</p>
  
  <p>In order to install the google api resources, I used composer with this command:</p>
  
  <pre><code>php composer.phar require google/apiclient:^2.0.0@RC
  </code></pre>
  
  <p>This installed the "vendor" folder in my root site folder. My index.php and oauth2callback.php files are located in the "public_html" folder.</p>
  
  <p>Here's a screenshot of my error when going to my site:</p>
  
  <p><a href="https://i.stack.imgur.com/Dsmvz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Dsmvz.jpg" alt="redirect_uri_mismatch"></a></p>
  
  <p>The weird thing is that if I navigate to the link above that's included in the error message "Visit ...... to update the authorized..", I get this error message: " The OAuth Client Does Not Exist "</p>
  
  <p><a href="https://i.stack.imgur.com/AFvf1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AFvf1.jpg" alt="The OAuth Client Does Not Exist"></a></p>
  
  <p>If I click on my only available Client ID, I can navigate to see the URI's which I'll screenshot below as well:</p>
  
  <p><a href="https://i.stack.imgur.com/v6PA1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v6PA1.jpg" alt="API Screen"></a></p>
  
  <p>As you can see, under Authorized Javascript origins, I have <a href="http://localhost" rel="nofollow noreferrer">http://localhost</a> listed, and under authorized redirect URIs, I have my live site followed by the "oauthc2callback.php" file extension. </p>
  
  <p>I don't understand how to get rid of the error I'm getting. I've tried replacing the URI's and putting in different JavaScript origins. </p>
  
  <p>Also, for some reason on that last screenshot, it says that I don't have permission to edit this OAuth client, but I can make edits.</p>
  
  <p>The code I have for index.php:</p>
  
  <pre><code>&lt;?php
  // Load the Google API PHP Client Library.
  require_once '../vendor/autoload.php';
  
  // Start a session to persist credentials.
  session_start();
  
  // Create the client object and set the authorization configuration
  // from the client_secretes.json you downloaded from the developer console.
  $client = new Google_Client();
  $client-&gt;setAuthConfigFile('../config/client_secrets.json');
  $client-&gt;addScope('https://www.googleapis.com/auth/analytics.readonly');
  
  // If the user has already authorized this app then get an access token
  // else redirect to ask the user to authorize access to Google Analytics.
  if (isset($_SESSION['access_token']) &amp;&amp; $_SESSION['access_token']) {
      // Set the access token on the client.
      $client-&gt;setAccessToken($_SESSION['access_token']);
  
      // Create an authorized analytics service object.
      $analytics = new Google_Service_Analytics($client);
  
      // Get the first view (profile) id for the authorized user.
      $profile = getFirstProfileId($analytics);
  
      // Get the results from the Core Reporting API and print the results.
      $results = getResults($analytics, $profile);
      printResults($results);
  } else {
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  }
  
  
  function getFirstprofileId(&amp;$analytics) {
      // Get the user's first view (profile) ID.
  
      // Get the list of accounts for the authorized user.
      $accounts = $analytics-&gt;management_accounts-&gt;listManagementAccounts();
  
      if (count($accounts-&gt;getItems()) &gt; 0) {
          $items = $accounts-&gt;getItems();
          $firstAccountId = $items[0]-&gt;getId();
  
          // Get the list of properties for the authorized user.
          $properties = $analytics-&gt;management_webproperties
          -&gt;listManagementWebproperties($firstAccountId);
  
          if (count($properties-&gt;getItems()) &gt; 0) {
              $items = $properties-&gt;getItems();
              $firstPropertyId = $items[0]-&gt;getId();
  
              // Get the list of views (profiles) for the authorized user.
              $profiles = $analytics-&gt;management_profiles
              -&gt;listManagementProfiles($firstAccountId, $firstPropertyId);
  
              if (count($profiles-&gt;getItems()) &gt; 0) {
                  $items = $profiles-&gt;getItems();
  
                  // Return the first view (profile) ID.
                  return $items[0]-&gt;getId();
  
              } else {
                  throw new Exception('No views (profiles) found for this user.');
              }
          } else {
              throw new Exception('No properties found for this user.');
          }
      } else {
          throw new Exception('No accounts found for this user.');
      }
  }
  
  function getResults(&amp;$analytics, $profileId) {
      // Calls the Core Reporting API and queries for the number of sessions
      // for the last seven days.
      return $analytics-&gt;data_ga-&gt;get(
      'ga:' . $profileId,
      '7daysAgo',
      'today',
      'ga:sessions');
  }
  
  function printResults(&amp;$results) {
      // Parses the response from the Core Reporting API and prints
      // the profile name and total sessions.
      if (count($results-&gt;getRows()) &gt; 0) {
  
          // Get the profile name.
          $profileName = $results-&gt;getProfileInfo()-&gt;getProfileName();
  
          // Get the entry for the first entry in the first row.
          $rows = $results-&gt;getRows();
          $sessions = $rows[0][0];
  
          // Print the results.
          print "&lt;p&gt;First view (profile) found: $profileName&lt;/p&gt;";
          print "&lt;p&gt;Total sessions: $sessions&lt;/p&gt;";
      } else {
          print "&lt;p&gt;No results found.&lt;/p&gt;";
      }
  }
  </code></pre>
  
  <p>The code I have for "oauth2callback.php":</p>
  
  <pre><code>&lt;?php
  require_once '../vendor/autoload.php';
  
  // Start a session to persist credentials.
  session_start();
  
  // Create the client object and set the authorization configuration
  // from the client_secrets.json you downloaded from the Developers Console.
  $client = new Google_Client();
  $client-&gt;setAuthConfigFile('../config/client_secrets.json');
  $client-&gt;setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
  $client-&gt;addScope('https://www.googleapis.com/auth/analytics.readonly');
  
  // Handle authorization flow from the server.
  if (! isset($_GET['code'])) {
      $auth_url = $client-&gt;createAuthUrl();
      header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
  } else {
      $client-&gt;authenticate($_GET['code']);
      $_SESSION['access_token'] = $client-&gt;getAccessToken();
      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  }
  </code></pre>
  
  <p>All of this code was taken from the first website example, except with a few minor additions to make it match my system.</p>
  
  <p>Anyone know how I can get rid of this error? What am I doing wrong?</p>