2013-10-01 32 views
12

我的应用使用Oauthed Cloud Endpoints并且在生产环境中正常工作。如何使用Oauth在devserver上测试云端点

我的问题是,在本地devserver,我的用户的用户总是被设置为[email protected],即使我已经通过正常的身份验证,访问代码,等等等等走了,有一个有效的authed用户。

我得到那个[email protected]对于oauth工作正常之前测试oauth端点是有用的,但是由于我的应用程序正在工作,我宁愿在那里看到实际的用户。

具体而言,我的终点的方法是

@ApiMethod(name = "insertEmp"), etc 
public Emp insertEmp(User user, Emp emp) { 
     System.out.println(user.getEmail()); // (A) log "appengine" email 
     System.out.println(OAuthServiceFactory.getOAuthService().getCurrentUser().getEmail(); // (B) log authed email 

     ... 

在部署时,一切都很好,都(A)和(B)的登录身份验证的用户([email protected])。 (A)总是记录“[email protected]”,尽管我已经通过Oauth序列并且拥有有效的,经过身份验证的用户,并且(B)记录了my.email @ gmail.com。因此,我可以进行高保真度测试,我需要用户成为真正的身份验证用户。

因此,简单地说,我如何得到(A)和(B)是相同的?

回答

3

似乎也不能这样做。通过将以下代码放在我的端点方法的顶部,我已经结束了编码。

if ("[email protected]".equalsIgnoreCase(user.getEmail()) { 
    user = new User(OAuthServiceFactory.getOAuthService().getCurrentUser().getEmail(),"foo"); 
} 

因此,现在即使在devserver上,用户电子邮件也与Oauth电子邮件相匹配。

+3

不适用于我...使用appengine 1.9.3和电子邮件继续相同:[email protected] –

+1

你仍然能够得到真正的电子邮件?我得到null作为用户,如果我调用OAuthServiceFactory.getOa ..()。getCurrentUser()我得到[email protected] – Patrick

+0

同样在这里。我为端点返回的用户以及OAuthServiceFactory.getOAuthService()获取了[email protected]。getCurrentUser() – Patrick

1

这并不容易。您必须在API控制台中进行设置。在这里,您将能够添加“本地主机”(http://localhost/)然后,您可以通过Google进行身份验证,即使您在本地主机上运行应用程序以进行开发。 我已经广泛使用它,它工作正常 链接:https://code.google.com/apis/console/ 只要记住你在这里使用的ID是完全独立于你的appengine ID。 花了我几个小时找出一个。

+2

谢谢,但这不是我问题的答案。我已经过去了将本地服务器配置到API控制台的问题,并且Oauth *正在工作。我的问题是,即使Oauth正在工作,Cloud Endpoints也会忽略它并将[email protected]替换为已通过身份验证的用户 – pinoyyid

+1

@pinoyyid:Google正在其文档中讨论此问题“重要提示:如果后端API受Auth保护,您的客户端必须提供支持,如上面在使用OAuth 2.0添加验证支持下所述,但在针对本地开发服务器运行时,不会显示凭据页面来响应用户的登录尝试,而只会出现登录,授予用户访问受保护的API以及返回的用户始终是[email protected]“,地址为https://cloud.google.com/appengine/docs/java/endpoints/consume_android#making_authenticated_calls – maya

0

问题是,当你在本地进行身份验证时,你没有通过Google服务器进行身份验证,因此对用户进行身份验证实际上不是在本地进行的。

当您尝试模拟登录时,Google总是提供[email protected]用户,它基本上发生在所有服务中,例如您在任何网站通过Google帐户提供登录(例如,使用GWT和App Engine)。

如果您与您的真实用户一起测试或者您认为[email protected]用户为您的用户,那么您的网站可能会有什么不同?

+1

但是... I **我**通过谷歌服务器。如果我从我的servlet调用oauth端点,我得到了真正的,经过认证的谷歌用户。问题在于devserver中的appengine/endpoint代码忽略了它。我无法进行音响测试,因为我想测试从未知用户接收用户的入门过程,以及具有能够访问Google API的存储凭证的入门过程。这是受阻的,因为我的CE端点类不是真正的用户,而是用假用户呈现。 – pinoyyid

+0

我已更新该问题以更详细地说明差异。 – pinoyyid

0

在您的端点API,你需要这个

ApiMethod (name="YourEndPointName", path="yourPath", 
      clientIds={"YourId.apps.googleusercontent.com"}, 
        scopes = { "https://www.googleapis.com/auth/userinfo.profile" }) 

然后在调用的方法,你将不得不从GAPI用户对象。 使用它从谷歌的用户对象获得实际的电子邮件这样

public myEndPointMethod(Foo foo, User user){ 
    email = user.getEmail(); 
} 
+0

请阅读该问题。我已经有了!问题是user.getEmail()返回的值。 – pinoyyid

+0

此外,范围应该在https://developers.google.com/+/api/oauth#profile中声明为“email”,因为userionfo.profile不包含用户的电子邮件,因此用户参数将为空。 +您提到的范围已被弃用。 – Pega88

+0

什么是'YourId'?我有myapp-xxxx.appspot.com - 我觉得我正在接近,但有点卡住 –

0

我替换了Oauth2用户(example @ example。com)与UserFactory的用户,它工作正常。我使用这种方法来验证所有API认证的API请求的用户。

public static User isAuthenticated(User user) throws OAuthRequestException{ 

    if(user == null){ 
     throw new OAuthRequestException("Please login before making requests"); 
    } 
    if(SystemProperty.environment.value() == 
      SystemProperty.Environment.Value.Development && "[email protected]".equalsIgnoreCase(user.getEmail())) { 

     //Replace the user from the user factory here. 
     user = UserServiceFactory.getUserService().getCurrentUser(); 

    } 
    return user; 

} 
+0

这不起作用。我从UserServiceFactory.getUserService()获取null。getCurrentUser()和OAuthServiceFactory.getUserService()获取[email protected] – Patrick

-1

这是不可能的。 我使用另一个端点替换当前会话中的user_id。

0

使用Go运行时我都使出这一功能以获得一个用户,既开发服务器和生产上的功能:

func currentUser(c context.Context) *user.User { 
    const scope = "https://www.googleapis.com/auth/userinfo.email" 
    const devClient = "123456789.apps.googleusercontent.com" 

    allowedClients := map[string]bool{ 
     "client-id-here.apps.googleusercontent.com": true, 
     devClient: true,    // dev server 
    } 

    usr, err := user.CurrentOAuth(c, scope) 
    if err != nil { 
     log.Printf("Warning: Could not get current user: %s", err) 
     return nil 
    } 

    if !allowedClients[usr.ClientID] { 
     log.Printf("Warning: Unauthorized client connecting with the server: %s", usr.ClientID) 
     return nil 
    } 

    if (usr.ClientID == devClient) { 
     usr = user.Current(c)   // replace with a more interesting user for dev server 
    } 
    return usr 
} 

这将使用开发服务器的登录信息输入使用http://localhost:8080/_ah/login

相关问题