Help with apiKey login
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...
Getting similar errors when using API.
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?
If that's the case, it's wrong. I am trying to use the adsense one and Key is not a good fit either.
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
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.
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.');
});
});
});
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");
});
});
});
So, it's working?
Yes, it's working :)
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?