Google Cloud Api中的用户为空

时间:2021-08-14 23:12:44

I followed the instructions in this tutorial.

我按照本教程中的说明操作。

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

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

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

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

我的helloworld.greetings.multiply和helloworld.greetings.getGreeting按预期工作。

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

但是我遇到了helloworld.greetings.authed方法的问题。

The user object is always null.

用户对象始终为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;
 }

}

3 个解决方案

#1


6  

I had same problem, it helped for me to add

我有同样的问题,这对我有帮助

scopes = {"https://www.googleapis.com/auth/userinfo.email"}

scopes = {“https://www.googleapis.com/auth/userinfo.email”}

into my Greetings @Api annotation. So the whole final @Apilook like

进入我的问候@Api注释。所以整个决赛@Apilook就像

@Api(
 name = "helloworld", 
 version = "v1", 
 clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID }, 
 scopes = {"https://www.googleapis.com/auth/userinfo.email"}
)

Then deploy, reload Api Explorer page and also turn on "Authorize requests using OAuth 2.0" option with same scope.

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

#2


1  

I am getting the same problem. And if you throw an OAuthRequestException Exception and test the service via the API Explorer console, you will get a message saying This method requires you to be authenticated. You may need to activate the toggle above to authorize your request using OAuth 2.0. When you try to enable the OAuth 2.0 toggle it requests in a new window to Select OAuth 2.0 scopes, and I have not been able to find which scopes are needed or figure out how I can test a cloud end-point service with authorization from the API Explorer console.

我遇到了同样的问题。如果您抛出OAuthRequestException异常并通过API Explorer控制台测试该服务,您将收到一条消息,说明此方法要求您进行身份验证。您可能需要激活上面的切换以使用OAuth 2.0授权您的请求。当您尝试启用OAuth 2.0在新窗口中将其请求切换为选择OAuth 2.0范围时,我无法找到所需的范围,或者弄清楚我如何使用授权来测试云端点服务API Explorer控制台。

#3


0  

First of all, in the API explorer, you need to authenticate the request with OAuth using the Authorize requests using OAuth 2.0 toggle in the user interface.

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

If the user is still null check that among the client ids there is the ID for the API explorer

如果用户仍为空,请检查客户端ID中是否存在API资源管理器的ID

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

This is the only thing that is needed to obtain a not null User argument.

这是获取非null User参数所需的唯一内容。

#1


6  

I had same problem, it helped for me to add

我有同样的问题,这对我有帮助

scopes = {"https://www.googleapis.com/auth/userinfo.email"}

scopes = {“https://www.googleapis.com/auth/userinfo.email”}

into my Greetings @Api annotation. So the whole final @Apilook like

进入我的问候@Api注释。所以整个决赛@Apilook就像

@Api(
 name = "helloworld", 
 version = "v1", 
 clientIds = { com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID }, 
 scopes = {"https://www.googleapis.com/auth/userinfo.email"}
)

Then deploy, reload Api Explorer page and also turn on "Authorize requests using OAuth 2.0" option with same scope.

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

#2


1  

I am getting the same problem. And if you throw an OAuthRequestException Exception and test the service via the API Explorer console, you will get a message saying This method requires you to be authenticated. You may need to activate the toggle above to authorize your request using OAuth 2.0. When you try to enable the OAuth 2.0 toggle it requests in a new window to Select OAuth 2.0 scopes, and I have not been able to find which scopes are needed or figure out how I can test a cloud end-point service with authorization from the API Explorer console.

我遇到了同样的问题。如果您抛出OAuthRequestException异常并通过API Explorer控制台测试该服务,您将收到一条消息,说明此方法要求您进行身份验证。您可能需要激活上面的切换以使用OAuth 2.0授权您的请求。当您尝试启用OAuth 2.0在新窗口中将其请求切换为选择OAuth 2.0范围时,我无法找到所需的范围,或者弄清楚我如何使用授权来测试云端点服务API Explorer控制台。

#3


0  

First of all, in the API explorer, you need to authenticate the request with OAuth using the Authorize requests using OAuth 2.0 toggle in the user interface.

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

If the user is still null check that among the client ids there is the ID for the API explorer

如果用户仍为空,请检查客户端ID中是否存在API资源管理器的ID

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

This is the only thing that is needed to obtain a not null User argument.

这是获取非null User参数所需的唯一内容。