Cha*_*enu 7 google-app-engine google-cloud-endpoints
我按照本教程中的说明操作.
https://developers.google.com/appengine/docs/java/endpoints/getstarted/auth
当我部署我的代码.并去测试我的应用程序.
使用以下网址
http://chandru-compute.appspot.com/_ah/api/explorer
我的helloworld.greetings.multiply和helloworld.greetings.getGreeting按预期工作.
但我对helloworld.greetings.authed方法有疑问.
用户对象始终为null.
这是代码.
package com.google.devrel.samples.helloendpoints;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import javax.inject.Named;
import java.util.ArrayList;
/**
* Defines v1 of a helloworld API, which provides simple "greeting" methods.
*/
@Api(
name = "helloworld",
version = "v1",
clientIds = {com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID}
)
public class Greetings {
public static ArrayList<Greeting> greetings = new ArrayList<Greeting>();
static {
greetings.add(new Greeting("hello world!"));
greetings.add(new Greeting("goodbye world!"));
}
public Greeting getGreeting(@Named("id") Integer id) {
return greetings.get(id);
}
@ApiMethod(name = "greetings.multiply", httpMethod = "post")
public Greeting insertGreeting(@Named("times") Integer times, Greeting greeting) {
Greeting response = new Greeting();
StringBuilder responseBuilder = new StringBuilder();
for (int i = 0; i < times; i++) {
responseBuilder.append(greeting.getMessage());
}
response.setMessage(responseBuilder.toString());
return response;
}
@ApiMethod(name = "greetings.authed", path = "greeting/authed")
public Greeting authedGreeting(User user) {
//Greeting response = new Greeting("hello " + user.getEmail());
Greeting response;
if (user == null) {
UserService userService = UserServiceFactory.getUserService();
User user2 = userService.getCurrentUser();
String text = null;
if (user2 != null){
text = user2.getEmail();
}
response = new Greeting("hello world : Email2" + text );
} else {
response = new Greeting("hello world : Email " + user.getEmail() );
}
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,这对我有帮助
scopes = {"https://www.googleapis.com/auth/userinfo.email"}
进入我的Greetings @Api注释.所以整个最终@Api看起来像
@Api(
name = "helloworld",
version = "v1",
clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID },
scopes = {"https://www.googleapis.com/auth/userinfo.email"}
)
Run Code Online (Sandbox Code Playgroud)
然后部署,重新加载Api Explorer页面,并打开相同范围的"使用OAuth 2.0授权请求"选项.
我遇到了同样的问题。如果您抛出OAuthRequestException异常并通过 API Explorer 控制台测试服务,您将收到一条消息,指出This method requires you to be authenticated. You may need to activate the toggle above to authorize your request using OAuth 2.0. 当您尝试启用 OAuth 2.0 切换时,它会在新窗口中请求选择 OAuth 2.0 范围,但我无法找到需要哪些范围,也无法弄清楚如何在获得授权的情况下测试云端点服务。 API 资源管理器控制台。
| 归档时间: |
|
| 查看次数: |
1822 次 |
| 最近记录: |