google-auto-auth error
Closed this issue · 2 comments
Anyone help on fixing this google auth error that is being generated, while using the emulator?
This is my index.js
file after the build
var googleAuth = require('google-auto-auth')();
//Handle Background events according to spec
function shimHandler(data) {
return new Promise((resolve, reject) => {
googleAuth.getToken(function (err, oauthToken) {
if (err) { console.log('googleAuth error')
reject()
} else {
const p = require('child_process').execFile('./dist/{{config["output_name"]}}/{{config["output_name"]}}', {
env: Object.assign(process.env, {
'GOOGLE_OAUTH_TOKEN': oauthToken,
})
});
var lastMessage;
p.stdin.setEncoding('utf-8');
//Log standard err messages to standard err
p.stderr.on('data', (err) => {
console.error(err.toString());
})
p.stdout.on('data', (out) => {
console.log(out.toString());
lastMessage = out;
})
p.on('close', (code) => {
if (code !== 0) {
//This means the shim failed / panicked. So we reject hard.
reject();
} else {
// Resolve the promise with the latest output from stdout
// In case of shimming http, this is the response object.
resolve(lastMessage);
}
});
//Write the object/message/request to the shim's stdin and signal
//End of input.
p.stdin.write(JSON.stringify(data));
p.stdin.end();
}
});
});
}
//Handle http request
function handleHttp(req, res) {
var requestBody;
console.log(req.get('content-type'))
switch (req.get('content-type')) {
case 'application/json':
requestBody = JSON.stringify(req.body);
break;
case 'application/x-www-form-urlencoded': //application/x-www-form-urlencoded
//The body parser for cloud functions does this, so just play along
//with it, sorry man! Maybe we should construct some kind of proper
//form request body? or not. let's keep it this way for now, as
//This is how cloud functions behaves.
//req.setHeader('content-type', 'application/json')
requestBody = JSON.stringify(req.body);
break;
case 'application/octet-stream':
requestBody = req.body;
break;
case 'text/plain':
requestBody = req.body;
break;
}
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
console.log('Request body ');console.log(JSON.stringify(requestBody));
console.log('Request headers');console.log(JSON.stringify(req.headers));
console.log('Request method'); console.log(JSON.stringify(req.method));
console.log('Request remote_addr'); console.log(JSON.stringify(req.ip));
console.log('Req protocol'); console.log(JSON.stringify(req.protocol));
console.log('Req get host'); console.log(JSON.stringify(req.get('host')));
console.log('originalUrl'); console.log(JSON.stringify(req.originalUrl));
var httpRequest = {
'body': requestBody,
'headers': req.headers,
'method': req.method,
'remote_addr': req.ip,
'url': fullUrl
};
shimHandler(httpRequest)
.then((result) => {
console.log('should come here')
data = JSON.parse(result);
res.status(data.status_code);
res.set(data.headers)
res.send(data.body);
})
.catch((e) => {
console.log(e);
console.log('some error has occurred');
res.status(500).end();
})
}
//{% if config["trigger_http"] %}
exports['{{config["function_name"]}}'] = function(req, res) {
return handleHttp(req, res);
}//{% else %}
exports['{{config["function_name"]}}'] = function(event, callback) {
return shimHandler(event.data).then(function() {
callback();
}).catch(function() {
callback(new Error("Function failed"));
});
}//{% endif %}
This is what I get on running logs using
functions logs read
Request body
2018-03-24T06:15:58.394Z - info: "{"refID":"50","refTable":"SubInspectionImages","image":"Amir"}"
Request headers
{"connection":"close","content-length":"48","accept-encoding":"gzip, deflate","host":"localhost:8010","accept":"/","user-agent":"PostmanRuntime/7.1.1","postman-token":"7f3d37c7-44be-4347-9af1-75dceb347277","cache-control":"no-cache","content-type":"application/x-www-form-urlencoded"}
Request method
"POST"
Request remote_addr
2018-03-24T06:15:58.394Z - info: "127.0.0.1"
Req protocol
2018-03-24T06:15:58.395Z - info: "http"
Req get host
"localhost:8010"
originalUrl
"/"
2018-03-24T06:15:58.405Z - info: googleAuth error
2018-03-24T06:15:58.406Z - info: undefined
2018-03-24T06:15:58.406Z - info: some error has occurred
This is my requirements.txt
altgraph==0.15
cachetools==2.0.1
certifi==2018.1.18
chardet==3.0.4
Django==1.11.1
future==0.16.0
google-auth==1.3.0
idna==2.6
Jinja2==2.9.6
jsonpickle==0.9.6
macholib==1.9
MarkupSafe==1.0
numpy==1.14.2
opencv-python==3.3.0.9
pefile==2017.11.5
pyasn1==0.4.2
pyasn1-modules==0.2.1
pycloudfn==0.1.209
PyInstaller==3.3.1
pyspin==1.1.1
python-dateutil==2.6.0
pytz==2018.3
requests==2.18.4
rsa==3.4.2
scipy==1.0.1
six==1.10.0
urllib3==1.22
Werkzeug==0.12
My pip crashes if I install pip install google-cloud-core
, I'm using python3.5
Have I missed any of the required library?
I did solve this google-auth error once I ran this gcloud auth application-default login
command Credit , Documentation
@MartinSahlen I'm a beginner in this google cloud itself. May be you can add this to your procedure of how to get google auth permission for beginner how would want to use this.