I have a google cloud endpoint api application which i want to call it from another google app engine web application, say from a servlet. The cloud endpoint api is not secured.
我有一个谷歌云端点api应用程序,我想从另一个谷歌应用程序引擎Web应用程序,从servlet说。云端点api不受保护。
I tried looking for examples but could not find one. I see example related to Android client. When tried using URLConnection it does not work, am not sure whether am doing correctly as well, sample codes or pointers will be of great help. I also checked the logs of cloud endpoint api to see if any requests are coming through, but i don't see any errors in the logs.
我试着寻找例子,却找不到一个。我看到与Android客户端相关的示例。当尝试使用URLConnection它不起作用,我不确定是否也正确,样本代码或指针将有很大帮助。我还检查了云端点api的日志,看看是否有任何请求通过,但我没有看到日志中的任何错误。
2 个解决方案
#1
0
I'm doing this as well, although with authentication. You can use the Jar generated by the endpoint API at MyAPI/build/libs/MyAPI-v1-SNAPSHOT.jar, just as you might in Android.
我也是这样做的,尽管有认证。您可以在MyAPI / build / libs / MyAPI-v1-SNAPSHOT.jar中使用端点API生成的Jar,就像在Android中一样。
Once you depend on that JAR, your code to build an API client should look something like that below. Mine is using OAuth authentication with a service account, which I'll leave in there because it was the more complicated part which you might eventually need. But without authentication you should just be able to set the credential to null.
一旦依赖该JAR,构建API客户端的代码应如下所示。我正在使用OAuth身份验证和服务帐户,我将留在那里,因为这是您最终可能需要的更复杂的部分。但是,如果没有身份验证,您应该只能将凭据设置为null。
HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
List<String> SCOPES = Arrays.asList(EMAIL_SCOPE);
String SERVICE_ACCCOUNT_ID = "my-account-id@my-app.iam.gserviceaccount.com";
String CREDENTIAL_FILE = "WEB-INF/my-file.p12";
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCCOUNT_ID)
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new File(CREDENTIAL_FILE))
.build();
MyAPI.Builder builder = new MyAPI.Builder(
HTTP_TRANSPORT,
JSON_FACTORY, credential)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://localhost:8080/_ah/api/")
// .setRootUrl("https://my-appengine-url.appspot.com/_ah/api")
MyAPI myAPI = builder.build();
#2
0
You cannot use URLConnection for making endpoint calls from App Engine. Use FetchURL
instead. If you set doNotFollowRedirect
, you will receive X-Appengine-Inbound-Appid
as header in the called App Engine project. You can trust this header, as Google would strip it off if somebody off App Engine would use it. Make sure you target the yourproject.appspot.com
domain, as it won't work with custom domains.
您不能使用URLConnection从App Engine进行端点调用。请改用FetchURL。如果设置了doNotFollowRedirect,您将在被调用的App Engine项目中收到X-Appengine-Inbound-Appid作为标头。你可以信任这个标题,因为如果有人关闭App Engine,谷歌会将其剥离。确保您定位yourproject.appspot.com域,因为它不适用于自定义域。
https://cloud.google.com/appengine/docs/java/appidentity/
https://cloud.google.com/appengine/docs/java/appidentity/
#1
0
I'm doing this as well, although with authentication. You can use the Jar generated by the endpoint API at MyAPI/build/libs/MyAPI-v1-SNAPSHOT.jar, just as you might in Android.
我也是这样做的,尽管有认证。您可以在MyAPI / build / libs / MyAPI-v1-SNAPSHOT.jar中使用端点API生成的Jar,就像在Android中一样。
Once you depend on that JAR, your code to build an API client should look something like that below. Mine is using OAuth authentication with a service account, which I'll leave in there because it was the more complicated part which you might eventually need. But without authentication you should just be able to set the credential to null.
一旦依赖该JAR,构建API客户端的代码应如下所示。我正在使用OAuth身份验证和服务帐户,我将留在那里,因为这是您最终可能需要的更复杂的部分。但是,如果没有身份验证,您应该只能将凭据设置为null。
HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
List<String> SCOPES = Arrays.asList(EMAIL_SCOPE);
String SERVICE_ACCCOUNT_ID = "my-account-id@my-app.iam.gserviceaccount.com";
String CREDENTIAL_FILE = "WEB-INF/my-file.p12";
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCCOUNT_ID)
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(new File(CREDENTIAL_FILE))
.build();
MyAPI.Builder builder = new MyAPI.Builder(
HTTP_TRANSPORT,
JSON_FACTORY, credential)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://localhost:8080/_ah/api/")
// .setRootUrl("https://my-appengine-url.appspot.com/_ah/api")
MyAPI myAPI = builder.build();
#2
0
You cannot use URLConnection for making endpoint calls from App Engine. Use FetchURL
instead. If you set doNotFollowRedirect
, you will receive X-Appengine-Inbound-Appid
as header in the called App Engine project. You can trust this header, as Google would strip it off if somebody off App Engine would use it. Make sure you target the yourproject.appspot.com
domain, as it won't work with custom domains.
您不能使用URLConnection从App Engine进行端点调用。请改用FetchURL。如果设置了doNotFollowRedirect,您将在被调用的App Engine项目中收到X-Appengine-Inbound-Appid作为标头。你可以信任这个标题,因为如果有人关闭App Engine,谷歌会将其剥离。确保您定位yourproject.appspot.com域,因为它不适用于自定义域。
https://cloud.google.com/appengine/docs/java/appidentity/
https://cloud.google.com/appengine/docs/java/appidentity/