google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

Help with apiKey login

Open huzjakd opened this issue 10 years ago • 10 comments

I'm pretty new to google apis, and am not sure if this is an error or I'm doing something wrong (It's probably my fault). I'm trying to get a simple API test working on my node server. This is what I have:

var google = require('googleapis');
google.options({ auth: '...myApiKey...' });
var androidpublisher = google.androidpublisher('v2');

app.get("/test3", function(req, res){
    //test googe apis
    var apiKey = "...myApiKey...";

    androidpublisher.edits.get({ editId: 1, packageName: "com.web2apptests"}, function(err, data){
        if(err){
            console.log("err:" + err);
            res.end("Look at the console.");
        }else{
            console.log(data);
            res.end("Look at the console.");
        }
    });
});

But I get the error: "Error: Login Required" I even tried with this line of code:

androidpublisher.edits.get({ auth: apiKey, editId: 1, packageName: "com.web2apptests"}, function(err, data){

What am I doing wrong? Is it possible to use androidpublisher with apiKey or does it have to be OAuth? Maybe I'm using the wrong key...

huzjakd avatar Oct 05 '16 11:10 huzjakd

Getting similar errors when using API.

eranbes avatar Oct 05 '16 19:10 eranbes

I've gotten OAuth2 authentication working following the example on the front page, but is it possible to use this without actual human input (he has to confirm to get the authorization code)? So only with server side code?

huzjakd avatar Oct 06 '16 09:10 huzjakd

If that's the case, it's wrong. I am trying to use the adsense one and Key is not a good fit either.

eranbes avatar Oct 06 '16 09:10 eranbes

Have you looked into using a Service Account? https://developers.google.com/identity/protocols/OAuth2ServiceAccount

https://github.com/google/google-api-nodejs-client#choosing-the-correct-credential-type-automatically

jmdobry avatar Oct 07 '16 23:10 jmdobry

I tryed it with a default service account and I get the error: The current user has insufficient permissions to perform the requested operation. I was creating a new edit.

huzjakd avatar Oct 10 '16 09:10 huzjakd

Try this:

var google = require('googleapis');
var androidpublisher = google.androidpublisher('v2');

/**
 * This assumes that you've set the following environment variables:
 * 
 * - GCLOUD_PROJECT - Your Google Project ID, e.g. my-awesome-project-123
 * - GOOGLE_APPLICATION_CREDENTIALS - The path to your Service Account key file, e.g. /path/to/key.json
 *
 * You don't have worry about these env vars if running on GAE Flex or a GCE
 * instance created with the proper scopes.
 */
function auth (callback) {
  google.auth.getApplicationDefault(function (err, authClient) {
    if (err) {
      callback(err);
      return;
    }

    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      authClient = authClient.createScoped([
        'https://www.googleapis.com/auth/androidpublisher'
      ]);
    }
    callback(null, authClient);
  });
}

app.get('/test3', function (req, res) {
  auth(function (err, authClient) {
    if (err) {
      console.log('err:' + err);
      res.end('Look at the console.');
      return;
    }

    androidpublisher.edits.get({
      auth: authClient,
      editId: 1,
      packageName: 'com.web2apptests'
    }, function (err, editResource) {
      if (err) {
        console.log('err:' + err);
        res.end('Look at the console.');
        return;
      }

      console.log(data);
      res.end('Look at the console.');
    });
  });
});

jmdobry avatar Oct 14 '16 22:10 jmdobry

I was clicking around a bit. Creating service accounts, adding permissions, downloading keys and in the end I ended up with the same settings as before (i think), but this time it's working, in response I get the ID and expiry time with this code:

app.get("/test3", function(req, res){
    var key = require(__dirname + "/keys.json");
    var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/androidpublisher'], null);
    jwtClient.authorize(function(err, tokens) {
        if (err) {
            console.log(err);
            res.end("Look at the console");
            return;
        }

        androidpublisher.edits.insert({ auth: jwtClient, packageName: "com.myproject"}, function(err, resp){
            console.log(err);
            console.log(resp);
            res.end("Look at the console");
        });
    });
});

huzjakd avatar Oct 17 '16 09:10 huzjakd

So, it's working?

jmdobry avatar Oct 17 '16 16:10 jmdobry

Yes, it's working :)

huzjakd avatar Oct 18 '16 07:10 huzjakd

I was clicking around a bit. Creating service accounts, adding permissions, downloading keys and in the end I ended up with the same settings as before (i think), but this time it's working, in response I get the ID and expiry time with this code:

app.get("/test3", function(req, res){
    var key = require(__dirname + "/keys.json");
    var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/androidpublisher'], null);
    jwtClient.authorize(function(err, tokens) {
        if (err) {
            console.log(err);
            res.end("Look at the console");
            return;
        }

        androidpublisher.edits.insert({ auth: jwtClient, packageName: "com.myproject"}, function(err, resp){
            console.log(err);
            console.log(resp);
            res.end("Look at the console");
        });
    });
});

Hi @huzjakd how can i get the private_key?

dapapkung avatar Mar 18 '20 13:03 dapapkung