title: request Google Analytics data from a local server
body: <p>I want to write a PHP script that imports web stats data from GA. The script is accessible through a web front end (for triggering the import) and resides on a local server (127.0.0.1).</p>
  
  <p><img src="https://i.stack.imgur.com/zlLMv.png" alt="enter image description here"></p>
  
  <p>As I understood from the documentation is that there are two options for authenticating and using the core API:</p>
  
  <ol>
  <li><strong>API key</strong> - grants only access to statistics</li>
  <li><strong>OAuth2</strong> - full authorization</li>
  </ol>
  
  <p>If I understand the mechanics of OAuth2 correctly then this is not an option in my scenario because I cannot specify a callback URL. Hacky solutions come to my mind - like establishing a web profile authentication directly connecting to GA from the browser and then fetching the data by JavaScript and feeding it to the import script - but I would prefer to refrain from such solutions. Also because the browser interaction triggering the import process might be substituted with a cron job in the future.</p>
  
  <p>The API key seems to be exactly what I want but the GET request from the browser fails. </p>
  
  <p><strong>GET request:</strong></p>
  
  <pre><code>https://www.googleapis.com/analytics/v3/data/ga
    ?ids=ga:[profile ID]
    &amp;start-date=2013-01-01&amp;end-date=2013-01-05
    &amp;metrics=ga:visits
    &amp;key=[the API key]
  </code></pre>
  
  <p><strong>Response:</strong></p>
  
  <pre><code>{
    error: {
    errors: [
      {
        domain: "global",
        reason: "required",
        message: "Login Required",
        locationType: "header",
        location: "Authorization"
      }
    ],
    code: 401,
    message: "Login Required"
    }
  }
  </code></pre>
  
  <p>The URL though should be fine. Except for the key parameter it is the same as the one generated with <a href="http://ga-dev-tools.appspot.com/explorer/" rel="nofollow noreferrer">http://ga-dev-tools.appspot.com/explorer/</a> which is also working (AOuth2 is used in that case). The API key is fresh.</p>
  
  <p>Then again generating a new API key confronts me with the next inconveniency which is that apparently the key is only valid for a day.</p>
  
  <hr>
  
  <p>So at the end of the day my question is this:</p>
  
  <p><strong>Is it possible to fetch data in the above described scenario without having to authenticate manually or generate API keys on a daily basis?</strong></p>