5
我定义了使用谷歌云端点一个简单的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/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 explorer
进行测试。我激活了OAuth 2.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)
我得到我的令牌,然后将它传递给'oauth2.tokeninfo'。我的电子邮件地址是“200”。 'issued_to'部分对应于clientId的'com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID'。 – Sydney
有趣的是,这似乎可能是一个错误。这种行为是发生在生产还是应用程序服务器?是一致还是间歇? – bossylobster
它只在开发应用程序服务器上,因为我没有在应用程序引擎上部署我的应用程序。这是一贯的,我可以一直重现。我应该在应用引擎问题跟踪器中创建一张票吗?或者你有什么想法,我可以检查,看看它是否真的是一个错误。我不知道它是否相关,但我使用'LocalServiceTestHelper'创建了一个简单的测试,并且在调用'userService.getCurrentUser()'时总是返回'null'。代码是'LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalUserServiceTestConfig())。setEnvIsAdmin(true).setEnvIsLoggedIn(true);' – Sydney