我已经通过Android Studio创建了一个Cloud Endpoint,作为Android项目的一部分。
我不打算将这部分Cloud Endpoint用于Android。
我需要使用cURL访问此Cloud Endpoint。我收到了错误:
404 - 未找到
Endpoint在线,因为我可以在通常的URL上看到index.html:
你好,终点!
输入您的姓名,然后按下面的按钮调用您的Google Cloud Endpoints API。
为了找到我使用了apis发现网址的网址:
https://appid.appspot.com/_ah/api/discovery/v1/apis
这提供了这个JSON(敏感数据混淆):
{
"kind": "discovery#directoryList",
"discoveryVersion": "v1",
"items": [
{
"kind": "discovery#directoryItem",
"id": "discovery:v1",
"name": "discovery",
"version": "v1",
"title": "APIs Discovery Service",
"description": "Provides information about other Google APIs, such as what APIs are available, the resource, and method details for each API.",
"discoveryRestUrl": "https://appid.appspot.com/_ah/api/discovery/v1/apis/discovery/v1/rest",
"discoveryLink": "./apis/discovery/v1/rest",
"icons": {
"x16": "http://www.google.com/images/icons/feature/filing_cabinet_search-g16.png",
"x32": "http://www.google.com/images/icons/feature/filing_cabinet_search-g32.png"
},
"documentationLink": "https://developers.google.com/discovery/",
"preferred": true
},
{
"kind": "discovery#directoryItem",
"id": "myApi:v1",
"name": "myApi",
"version": "v1",
"description": "This is an API",
"discoveryRestUrl": "https://appid.appspot.com/_ah/api/discovery/v1/apis/myApi/v1/rest",
"discoveryLink": "./apis/myApi/v1/rest",
"icons": {
"x16": "https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png",
"x32": "https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png"
},
"preferred": true
}
]
}
我将discoveryRestUrl
用于api myApi
。
https://appid.appspot.com/_ah/api/discovery/v1/apis/myApi/v1/rest
{
"kind": "discovery#restDescription",
"etag": "\"ODRTh3xaRR64wpeXJSlF33HMN-0/uPLUFYnKhGLgMnhurkbMaKcMDpA\"",
"discoveryVersion": "v1",
"id": "myApi:v1",
"name": "myApi",
"version": "v1",
"description": "This is an API",
"ownerDomain": "backend.appid.companydomain.com",
"ownerName": "backend.appid.companydomain.com",
"icons": {
"x16": "https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png",
"x32": "https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png"
},
"protocol": "rest",
"baseUrl": "https://appid.appspot.com/_ah/api/myApi/v1/sayHi/",
"basePath": "/_ah/api/myApi/v1/sayHi/",
"rootUrl": "https://appid.appspot.com/_ah/api/",
"servicePath": "myApi/v1/sayHi/",
"batchPath": "batch",
"parameters": {
"alt": {
"type": "string",
"description": "Data format for the response.",
"default": "json",
"enum": [
"json"
],
"enumDescriptions": [
"Responses with Content-Type of application/json"
],
"location": "query"
},
"fields": {
"type": "string",
"description": "Selector specifying which fields to include in a partial response.",
"location": "query"
},
"key": {
"type": "string",
"description": "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
"location": "query"
},
"oauth_token": {
"type": "string",
"description": "OAuth 2.0 token for the current user.",
"location": "query"
},
"prettyPrint": {
"type": "boolean",
"description": "Returns response with indentations and line breaks.",
"default": "true",
"location": "query"
},
"quotaUser": {
"type": "string",
"description": "Available to use for quota purposes for server-side applications. Can be any arbitrary string assigned to a user, but should not exceed 40 characters. Overrides userIp if both are provided.",
"location": "query"
},
"userIp": {
"type": "string",
"description": "IP address of the site where the request originates. Use this if you want to enforce per-user limits.",
"location": "query"
}
},
"auth": {
"oauth2": {
"scopes": {
"https://www.googleapis.com/auth/userinfo.email": {
"description": "View your email address"
}
}
}
},
"schemas": {
"MyBean": {
"id": "MyBean",
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
},
"methods": {
"sayHi": {
"id": "myApi.sayHi",
"path": "{name}",
"httpMethod": "POST",
"parameters": {
"name": {
"type": "string",
"required": true,
"location": "path"
}
},
"parameterOrder": [
"name"
],
"response": {
"$ref": "MyBean"
},
"scopes": [
"https://www.googleapis.com/auth/userinfo.email"
]
}
}
}
我从这里获取了网址:"baseUrl": "https://appid.appspot.com/_ah/api/myApi/v1/sayHi/"
。
我的卷曲命令:
curl --header "Content-Type: application/json" -X POST -d '{"name": "Testing"}' http://appid.appspot.com/_ah/api/myApi/v1/sayHi
我应该使用什么网址?是否更有可能没有正确设置某些内容?
使用以下网址进行测试:
http://appid.appspot.com/myApi/v1/sayHi
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>404 Not Found</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Not Found</h1>
<h2>The requested URL <code>/myApi/v1/sayHi</code> was not found on this server.</h2>
<h2></h2>
</body></html>
我试验了这个端点:
/**
* A simple endpoint method that takes a name and says Hi back
*/
@ApiMethod(name = "sayHi2", path = "test")
public MyBean sayHi2() {
MyBean response = new MyBean();
response.setData("This is a message");
return response;
}
我可以这样称呼:
curl --header "Content-Type: application/json" -X POST https://appid.appspot.com/_ah/api/myApi/v1/test
我仍然无法解释为什么无法访问原始端点,但如果添加路径注释会修复它,那么我很高兴。
答案 0 :(得分:1)
这是因为@Named("name")
这里是路径参数。请注意发现文档的这一部分:
"sayHi": {
"id": "myApi.sayHi",
"path": "{name}",
这意味着要调用此方法,请将名称附加到基本路径,因此正确的命令是:
$ curl --header "Content-Type: application/json" -X POST -d '' https://appid.appspot.com/_ah/api/myApi/v1/sayHi/test
{
"data": "Hi, test",
"kind": "myApi#resourcesItem",
"etag": "\"yN6wlOVUuMN6KwnZ3LSrHdbVqwM/EMLxMt_QPFPG4ZrTIVpiyuCWflg\""
}