【问题标题】:How to add authentication to google cloud endpoints?如何向谷歌云端点添加身份验证?
【发布时间】:2013-04-02 01:32:13
【问题描述】:

从 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!

现在尝试以井字游戏应用为例添加身份验证。

@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" and

一般来说,我们如何向“App Engine Connected Android Project”生成的端点添加身份验证?

来自 web 应用程序、android 客户端或 iOS 客户端,我们可以通过 api explorer 对其进行测试吗?

【问题讨论】:

  • Ids.*_CLIENT_ID 使用什么值?
  • +Drux 感谢您的提问,但我继续前进并使用了一个有效的 python 解决方案,因此无需回答这个问题。

标签: java android google-app-engine google-oauth google-cloud-endpoints


【解决方案1】:

“一般来说,我们如何向“App Engine Connected Android Project”生成的端点添加身份验证?”

执行此操作的说明在这里:
https://developers.google.com/appengine/docs/java/endpoints/consume_android#making-authenticated-calls

您还将在此处(在后续页面上)看到有关 iOS 和 Javascript 的说明。

在服务器端,您需要添加 User 对象作为端点方法的第一个参数。

我不知道你其他问题的答案。

【讨论】:

  • 谢谢汤姆。我错过了tic tac toe示例中的User参数,所以我添加了它,我仍然看到错误,例如“com.google.api.server.spi.SystemService invokeServiceMethod: null java.lang.ExceptionInInitializerError” 感谢您的帮助。如果您或任何人对调试此类错误和测试的最佳实践有建议,请发布。
  • 我认为错误是 Android 运行时错误。我所能建议的只是这表明您在调用服务之前还没有完成所有需要的事情。您需要发布更多信息,这可能应该是一个单独的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2019-11-01
  • 2018-10-20
相关资源
最近更新 更多