[英]Appengine: call endpoints of another module on the same server
我正在使用目前有2個模塊的App Engine服務器。 我有用於端點和同步模塊的默認模塊。 第二個模塊用於將我的服務器與另一個模塊同步。 所以我的同步模塊從其他服務器獲取數據,並且必須使用端點將其發送到默認模塊。 為此,我生成了endpoints_client_library並將庫添加到我的同步模塊。 我嘗試了很多案例,但我無法正確地傳達我的終點。 每次我都會收到像“401 Unauthorized”這樣的錯誤。
所以我不知道在我的服務器上使用生成的端點客戶端庫是否正確,或者是否有其他解決方案,可能更簡單......
我只想將同步模塊中的數據發送到默認值。
如果你需要一些代碼,即使它不是很完整而且根本不工作,只需說出來即可。
我正在使用的URLFetch代碼:
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);
結果如下:
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}"
}
}
遵循Java中App Engine模塊中模塊 之間通信的一般准則。 在第二個代碼示例之后,它表示您還可以使用URL Fetch Service進行異步流控制。
為了安全起見,文檔繼續為被調用模塊(您的端點)提供建議,以對請求進行身份驗證。 手動或基本擴展管理員登錄解決方案通常不起作用,因為默認情況下自動縮放。 在您的方案中,Inbound-AppId標頭將是一個非常簡單和安全的選擇。
正如您願意為您的問題添加更多詳細信息一樣,我很樂意嘗試為此答案添加更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.