Java 对端点使用Auth
我使用Google云端点定义了一个简单的API:Java 对端点使用Auth,java,google-app-engine,google-cloud-endpoints,Java,Google App Engine,Google Cloud Endpoints,我使用Google云端点定义了一个简单的API: @Api(name = "realestate", version = "v1", clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID }, scopes = { "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/aut
@Api(name = "realestate", version = "v1", clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID }, scopes = {
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile" })
public class RealEstatePropertyV1 {
@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstateProperty property, User user)
throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException("Must log in");
}
System.out.println(user.getEmail());
}
}
然后我尝试使用API资源管理器
对其进行测试。我激活了OAuth2.0。但是当我执行请求时,User
对象是null
Jun 23, 2013 10:21:50 AM com.google.appengine.tools.development.DevAppServerImpl start
INFO: Dev App Server is now running
Jun 23, 2013 10:22:42 AM com.google.api.server.spi.SystemServiceServlet init
INFO: SPI restricted: true
Jun 23, 2013 10:22:43 AM com.google.api.server.spi.WebApisUserService getCurrentUser
WARNING: getCurrentUser: clientId not allowed
Jun 23, 2013 10:22:43 AM com.google.api.server.spi.SystemService invokeServiceMethod
INFO: cause={0}
com.google.api.server.spi.response.UnauthorizedException: Must log in
at com.realestate.api.v1.RealEstatePropertyV1.create(RealEstatePropertyV1.java:44)
消息
getCurrentUser:clientId not allowed
表示与令牌关联的客户端ID是空字符串。这似乎不可能,可能是一个奇怪的错误/怪癖
您应该检查请求中发送的令牌,它将出现在类似的request
部分
GET https://www.googleapis.com/oauth2/v2/userinfo?key={YOUR_API_KEY}
Authorization: Bearer ya29....
X-JavaScript-User-Agent: Google APIs Explorer
您的令牌以ya29.
开头。您应该通过将令牌信息发送到令牌信息API中来确保令牌信息已签出:
https://developers.google.com/apis-explorer/#p/oauth2/v2/oauth2.tokeninfo
我得到了我的令牌,然后将其传递给
oauth2.tokeninfo
。回复是200
,带有我正确的电子邮件。发布到的部分对应于clientIdcom.google.api.server.spi.Constant.api\u EXPLORER\u CLIENT\u ID
。有趣的是,这似乎是一个bug。这种行为是在生产环境中还是在开发应用服务器中发生的?它是一致的还是断断续续的?它只在dev appserver上,因为我没有在app engine上部署我的应用。它是一致的,我可以一直复制。我是否应该在应用程序引擎问题跟踪器中创建一个票证?或者你知道我可以检查什么来确定它是否真的是一个bug吗。我不知道它是否相关,但我使用localservicesthelper
创建了一个简单的测试,调用userService.getCurrentUser()
时,它总是返回null
。代码是LocalServiceTestHelper helper=newLocalServiceTestHelper(newLocalUserServiceTestConfig()).setEnvIsAdmin(true).setEnvIsLoggedIn(true)代码>我切换回1.8.0,现在它可以工作了。我创建了一个问题,因为它不适用于1.8.1.1。我有类似的问题-从1.8.2切换到1.8.0解决了这个问题。谢谢