2

I have an app deployed to App Engine and would like to access the datastore for that app from a Node.js app that I am running locally. I started with the Hello World app for Node and am trying to retrieve an entity from my deployed app's datastore and log it to the console following this documentation. My code for the Node app is below.

var express = require('express');
var gcloud = require('gcloud')({
  projectId: 'myProjectId',
  keyFilename: __dirname + 'myKeyfile.json'
});
var datastore = gcloud.datastore;
var dataset = datastore.dataset();

var app = express();

app.get('/', function(req, res) {
  dataset.get(dataset.key(['Practice', 4392384]), function(err, entity) {
    if (err) {
      console.log("ERROR: " + err);
    } else {
      console.log("Practice entity: " + entity);
    }
  });
  res.status(200).send('Hello, world!');
});    

var server = app.listen(process.env.PORT || 8081, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('App listening at http://%s:%s', host, port);
});

I have set up a service account in my App Engine app following these directions (under the "Accessing an existing App Engine Datastore from another platform" header) and generated a JSON key file that I have in my Node project's root directory (same directory as app.js). When attempting to get a valid entity from the datastore, I log the error which is "ApiError: Forbidden". I can only speculate that the App Engine project is not properly configured with the service account or the Cloud Datastore API not properly enabled, so is there anything I'm obviously doing wrong, or any way I can get more details about the error being returned?

Daniel Allen
  • 894
  • 1
  • 10
  • 23
  • Just confirming that I currently have exactly the same problem. Tried locally with Node.js 0.10, 0.12 and 4.2.3, and also with some older versions of gcloud. Maybe this is a temporary problem in Google Cloud? – Kennu Jan 15 '16 at 00:08

2 Answers2

1

I had the same problem for a duration of a few hours (during Jan 15 2016). Then the Node.js code started working again without changing anything and I'm now able to access the Datastore API. Apparently it was a temporary problem in Google's cloud service.

Kennu
  • 1,093
  • 9
  • 13
  • 1
    Unfortunately I'm still experiencing the same issue today so I suspect something else may be wrong with my configuration. – Daniel Allen Jan 15 '16 at 13:13
  • 1
    For what it's worth, the App Engine app was created several years ago, so I'm unsure if the [limitation](https://cloud.google.com/datastore/docs/activate) of the project being created with the older Google APIs console is applicable; the documentation is vague in this area. – Daniel Allen Jan 15 '16 at 13:48
  • One thing I can mention is that my application originally had a callback problem, causing it to fire a lot of unthrottled Google API requests. I guess the Forbidden error could also have been a throttling limit, although I didn't see any indication of that in the API console or in the error messages. – Kennu Jan 16 '16 at 13:35
1

I received help from Google's technical support on this issue. It turns out I was trying to implement a sample that wasn't intended for a local Node app connecting to a deployed App Engine app. The service account I was trying to use was for connecting the client-side of Node.js to the backend of the app, but not accessing one application from another (which is what I was doing). In order to do what I intended, he recommended following these instructions.

Daniel Allen
  • 894
  • 1
  • 10
  • 23