从 Google Cloud Endpoint 开始,tic tac toe 示例(请参阅https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-java)
然后,在最新的 Android Ellipse 插件中通过 Ellipse 创建一个“AppEngine Connected Android Project”(参见https://developers.google.com/eclipse/docs/endpoints-androidconnected-gae)
现在这会创建没有身份验证的端点。
// @Api(name = "messageEndpoint")
//NO AUTHENTICATION; OPEN ENDPOINT!
现在尝试使用 tic tac toe 应用程序作为示例添加身份验证。
@Api(name = "messageEndpoint",
version = "v1",
clientIds = {Ids.WEB_CLIENT_ID, Ids.ANDROID_CLIENT_ID, Ids.IOS_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID},
audiences = {Ids.ANDROID_AUDIENCE}
)
如果出现 oauth 异常,请添加以下内容:
//public void sendMessage(@Named("message") String message)
//throws IOException {
public void sendMessage(@Named("message") String message)
throws OAuthRequestException,IOException {
并使用 api explorer https://[your-app-id].appspot.com/_ah/api/explorer 进行测试,我将 [your-app-id] 替换为我的特定应用程序 ID。
POST https://[your-app-id].appspot.com/_ah/api/messageEndpoint/v1/sendMessage/test
X-JavaScript-User-Agent: Google APIs Explorer
503 Service Unavailable
cache-control: private, max-age=0
content-encoding: gzip
content-length: 193
content-type: application/json; charset=UTF-8
date: Mon, 01 Apr 2013 22:57:17 GMT
expires: Mon, 01 Apr 2013 22:57:17 GMT
server: GSE
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "java.lang.NoClassDefFoundError: Could not initialize class com.brooklynmarathon.citysync.endpoints.EMF"
}
],
"code": 503,
"message": "java.lang.NoClassDefFoundError: Could not initialize class com.brooklynmarathon.citysync.endpoints.EMF"
}
}
我尝试使用我下载的井字游戏应用程序(感谢 Google Dev Rel)并在此处构建和上传:https ://brooklynmarathon.appspot.com/_ah/api/explorer
当我尝试访问分数列表时,我看到:
GET https://brooklynmarathon.appspot.com/_ah/api/tictactoe/v1/score
X-JavaScript-User-Agent: Google APIs Explorer
Response
401 Unauthorized
- Hide headers -
cache-control: private, max-age=0
content-encoding: gzip
content-length: 193
content-type: application/json; charset=UTF-8
date: Tue, 02 Apr 2013 01:14:40 GMT
expires: Tue, 02 Apr 2013 01:14:40 GMT
server: GSE
www-authenticate: GoogleLogin realm="https://www.google.com/accounts/ClientLogin", service="xapi"
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "com.google.appengine.api.oauth.OAuthRequestException: Invalid user.",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "com.google.appengine.api.oauth.OAuthRequestException: Invalid user."
}
}
我的问题是为什么井字游戏示例有标题:“www-authenticate: GoogleLogin realm="https://www.google.com/accounts/ClientLogin", service="xapi" 和
一般来说,我们如何向“App Engine Connected Android Project”生成的端点添加身份验证?
从 web 应用程序、android 客户端或 iOS 客户端,我们可以从 api explorer 测试它吗?