I am working on an App Engine server which has currently 2 modules. I have the default module used for endpoints and the sync module. This second module is used for syncing my server with another. So my sync module gets data from other servers and has to send it to the default module using endpoints. To do this, I generated endpoints_client_library and added the library to my sync module. I tried a lot of cases but I can't communicate my endpoints properly. Each times I got errors like "401 Unauthorized".
So I don't know if it's the right way to use the generated endpoints client library on my server or if there is another solution, maybe simplier...
I just want to send data from my sync module to the default.
If you need some code, even if it is not very complete and not working at all, just say and I will.
The URLFetch code I'm using:
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
AppIdentityService.GetAccessTokenResult accessToken = appIdentity.getAccessToken(Collections.singleton(scope));
URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
FetchOptions options = FetchOptions.Builder.doNotFollowRedirects().disallowTruncate();
HTTPRequest request = new HTTPRequest(url, HTTPMethod.GET, options);
HTTPHeader userAgent = new HTTPHeader("User-Agent", "AppEngine-Google; (+http://code.google.com/appengine; appid: appId)");
request.addHeader(userAgent);
HTTPHeader autho = new HTTPHeader("Authorization", "OAuth "+accessToken.getAccessToken());
request.addHeader(autho);
HTTPHeader contentType = new HTTPHeader("Content-Type", "application/json");
request.addHeader(contentType);
HTTPResponse response = fetcher.fetch(request);
int code = response.getResponseCode();
String resp = new String(response.getContent());
System.out.println(code);
System.out.println(resp);
And the result:
401
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "{\"class\":\"com.domain.server.cloud.CloudException\",\"code\":2}",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "{\"class\":\"com.domain.server.cloud.CloudException\",\"code\":2}"
}
}