7

I followed the instructions in this tutorial.

https://developers.google.com/appengine/docs/java/endpoints/getstarted/auth

when i deployed my code. and went to test my app.

with the following url

http://chandru-compute.appspot.com/_ah/api/explorer

My helloworld.greetings.multiply and helloworld.greetings.getGreeting works as expected.

But i have issues with the helloworld.greetings.authed method.

The user object is always null.

Here is the code.

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;
 }

}
4

3 回答 3

6

我有同样的问题,它帮助我添加

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"}
)

然后部署,重新加载 Api Explorer 页面,并打开具有相同范围的“使用 OAuth 2.0 授权请求”选项。

于 2013-08-24T07:08:57.080 回答
1

我遇到了同样的问题。如果您抛出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 资源管理器控制台。

于 2013-08-12T04:22:50.830 回答
0

首先,在 API 资源管理器中,您需要使用用户界面中的使用 OAuth 2.0 授权请求切换来使用 OAuth 对请求进行身份验证。

如果用户仍然为空,请检查客户端 ID 中是否存在 API Explorer 的 ID

@Api(
  name = "myAPIName", 
  version = "v1", 
  clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID }
)

这是获取非空用户参数所需的唯一内容。

于 2015-04-14T20:31:02.130 回答