Skip to content

Datastore API auth depends on being logued with the gcloud CLI (remote) #1168

Closed

Description

@sespinosa

This code only works if you are logued using gcloud cli.

I we are using credentials for this (keyFile.json) its kinda weird to also need to log in on the gcloud cli.

var gcloud = require('gcloud');

// Authenticating on a per-API-basis. You don't need to do this if you auth on a
// global basis (see Authentication section above).

var dataset = gcloud.datastore.dataset({
  projectId: 'central-station-staging',
  keyFilename: './keyFile.json'
});

dataset.get(dataset.key(['Product', 'Computer']), function(err, entity) {
  console.log(err || entity);
});

// Save data to your dataset.
var blogPostData = {
  title: 'How to make the perfect homemade pasta',
  author: 'Andrew Chilton',
  isDraft: true
};

var blogPostKey = dataset.key('BlogPost');

dataset.save({
  key: blogPostKey,
  data: blogPostData
}, function(err) {
  // `blogPostKey` has been updated with an ID so you can do more operations
  // with it, such as an update.
  blogPostData.isDraft = false;

  dataset.save({
    key: blogPostKey,
    data: blogPostData
  }, function(err) {
    if (!err) {
      // The blog post is now published!
    }
  });
});

Activity

  1. stephenplusplus commented on Mar 15, 2016

    @stephenplusplus
    Contributor

    Can you show the exact code you run and the error you are getting?

  2. sespinosa commented on Mar 15, 2016

    @sespinosa
    Author

    thats the exact code, when i run that on a env with the gcloud cli installed and logued it runs with no problem and adds the blogposts to the db

  3. stephenplusplus commented on Mar 16, 2016

    @stephenplusplus
    Contributor

    Okay, let's narrow the scope to one specific call:

    var gcloud = require('gcloud');
    
    // Authenticating on a per-API-basis. You don't need to do this if you auth on a
    // global basis (see Authentication section above).
    
    var dataset = gcloud.datastore.dataset({
      projectId: '...',
      keyFilename: '...'
    });
    
    // Save data to your dataset.
    var blogPostData = {
      title: 'How to make the perfect homemade pasta',
      author: 'Andrew Chilton',
      isDraft: true
    };
    
    var blogPostKey = dataset.key('BlogPost');
    
    dataset.save({
      key: blogPostKey,
      data: blogPostData
    }, console.log);

    Here are my results:

    $ gcloud auth revoke
    $ node .
    null { mutation_result: { index_updates: 7, insert_auto_id_key: [ [Object] ] } }
    

    This also works for our integration tests, which runs our Datastore tests on a short-lived, Travis-created Linux VM without the gcloud SDK installed.

    Do you have a local Datastore instance running? And what error is being returned when you run this code after gcloud revoke?

  4. sespinosa commented on Mar 16, 2016

    @sespinosa
    Author

    My env its a VM with gcloud cli not installed, im connecting remotely (im running my app locally and i access the datastore on gae) and im getting "Error: invalid_grant" all the time, but when i run this outside the VM, with gcloud cli installed and logued with a valid session works like a charm.

  5. stephenplusplus commented on Mar 16, 2016

    @stephenplusplus
    Contributor

    Would you mind showing the output of:

    console.trace(err);

    The invalid_grant error is returned when trying to get an authorization token. I'm not sure why that would happen in one environment with the same project ID and key file and not another. Googling around shows that it could be an issue with the VM's time being out of sync with the Google server. Depending on your OS, maybe something like this is worth a shot: googleapis/google-api-ruby-client#84 (comment)?

  6. sespinosa commented on Mar 16, 2016

    @sespinosa
    Author

    man, you are the coolest

    running sudo ntpdate ntp.ubuntu.com solve my problem!

    im really sorry for wasting your time.

  7. stephenplusplus commented on Mar 16, 2016

    @stephenplusplus
    Contributor

    Not at all, I'm glad it worked!

  8. added a commit that references this issue on Jan 28, 2026
  9. added a commit that references this issue on Feb 5, 2026
  10. added a commit that references this issue on Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions