In my Google Endpoints project, I need custom headers because I use custom authentication. I do this on the backend-side (endpoints code) as described in this StackOverflow answer. Do get this working on the Android client I simply had to send the extra headers to the endpoints service like this:
Backend.Builder pb = new Backend.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
HttpHeaders headers = request.getHeaders();
headers.set("authstuff", usernameTxt+"&"+passwdTxt);
request.setHeaders(headers);
}
});
In the JavaScript code, there doesn't seem to be any way of sending headers with the gapi.client.load-action, so I've tried to make a request right after the loading is done. I've tried several versions of the below:
gapi.client.load('backend', 'v1', null, 'http://localhost:8080/_ah/api').then(function () {
gapi.client.request({
root: 'http://localhost:8080/_ah/api',
path: '/backend/v1/',
headers: {'authstuff': 'user&password'}
}).then(function(res) {
console.log(res);
}, function(res) {
console.log(res);
});
});
One idea I got after getting 404 errors was to send the authheader (authstuff) to one of the REST endpoints (let's say that userinfo/userid may be one of them). This gives me 503 service unavailable errors client side, and on the server, it seems that the authstuff header isn't included at all. (In other words replacing path above with /backend/v1/userinfo/1/ for instance).
My question is really; do any of you have any way of including the necessary headers so the authentication way in the StackOverflow link above work? I know the easiest way would be to use Google accounts, but that isn't really an option. It seems weird to me that this is so easy in the Android Java version, but is hard in javascript.