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?
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.