title: Invalid credentials - 401 when trying to access Google Datastore from a GCE instance
body: <p>I'm trying the adams.py example found on this URL
  <a href="https://cloud.google.com/datastore/docs/getstarted/start_python/" rel="nofollow noreferrer">https://cloud.google.com/datastore/docs/getstarted/start_python/</a></p>
  
  <p>As it's explained in this URL:</p>
  
  <blockquote>
    <p>If you are running this code on Google Compute Engine, you do not have to create new service account credentials because the default service account for your project is used when you create an instance with the datastore and userinfo-email scopes. </p>
  </blockquote>
  
  <p>While creating my instance, I checked the option:</p>
  
  <blockquote>
    <p>Allow API access to all Google Cloud services in the same project.</p>
  </blockquote>
  
  <p>When I run </p>
  
  <blockquote>
    <p>python adams.py my-instance</p>
  </blockquote>
  
  <p>I get:</p>
  
  <pre><code>    ERROR:root:Error while doing datastore operation
      ERROR:root:RPCError: beginTransaction Invalid Credentials
      ERROR:root:HTTPError: 401 Unauthorized
  </code></pre>
  
  <p>In fact I get the same message even if I use a wrong name for the instance.</p>
  
  <p>My problem seems similar to this thread from one year ago: <a href="https://stackoverflow.com/questions/21901610/connect-google-datastore-from-existing-google-compute-engine-in-python">Connect google datastore from existing google compute engine in python</a></p>
  
  <p>In my own code, it works fine accessing BigQuery and then when it comes to Datastore it raises the same error, but at least is more specific addressing the line, which is the last one: resp = datastore.commit(req). Here is the section of the code use Datastore.</p>
  
  <pre><code>req = datastore.CommitRequest()
  req.mode = datastore.CommitRequest.NON_TRANSACTIONAL
  dsDashboard = req.mutation.insert_auto_id.add()
  
  path_element = dsDashboard.key.path_element.add()
  path_element.kind = 'Dashboard'
  fmt = '%Y-%m-%d'
  dashboardDate = dsDashboard.property.add()
  dashboardDate.name = 'Date'
  dashboardDate.value.string_value = dtDay.strftime(fmt)
  
  dashboardValue = dsDashboard.property.add()
  dashboardValue.name = 'Value'
  dashboardValue.value.indexed = False
  dashboardValue.value.string_value = encode(jDashboard)
  
  resp = datastore.commit(req)
  </code></pre>
  
  <p>The error for my code:</p>
  
  <pre><code>    Traceback (most recent call last):
        File "code.py", line 262, in &lt;module&gt;
          main()
        File "code.py", line 256, in main
          resp = datastore.commit(req)
        File "/usr/local/lib/python2.7/dist-packages/googledatastore/__init__.py", line 90, in commit
          return get_default_connection().commit(request)
        File "/usr/local/lib/python2.7/dist-packages/googledatastore/connection.py", line 135, in commit
          datastore_v1_pb2.CommitResponse)
        File "/usr/local/lib/python2.7/dist-packages/googledatastore/connection.py", line 195, in _call_method
          raise RPCError(method, response, content)
      googledatastore.connection.RPCError: commit RPC client failure with HTTP(401) Unauthorized: Invalid Credentials    
  </code></pre>
  
  <p>In short, it seems something simple to do. Anyone here had the same problem and has found a workaround?</p>