13

我的应用程序使用 Oauthed Cloud Endpoints,并且在生产中运行良好。

我的问题是,在本地开发服务器上,我的 User 用户始终设置为example@example.com,即使我已经完成了通常的身份验证、访问代码等,并且拥有一个有效的身份验证用户。

我知道 example@example.com 在我让 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) 都记录了经过身份验证的用户 (my.email@gmail.com)。

在我的本地开发服务器上进行测试时,(A)总是记录“example@example.com”,即使我已经完成了 Oauth 序列并且有一个有效的、经过身份验证的用户,并且(B)记录了 my.email@gmail.com。所以我可以做高保真测试,我需要用户是真正的认证用户。

所以简单来说,我如何让 (A) 和 (B) 相同?

4

7 回答 7

4

好像做不到。通过将以下代码放在我的 Endpoint 方法的顶部,我最终围绕它进行了编码。

if ("example@example.com".equalsIgnoreCase(user.getEmail()) {
    user = new User(OAuthServiceFactory.getOAuthService().getCurrentUser().getEmail(),"foo");
}

所以现在,即使在 devserver 上,用户电子邮件也与 Oauth 电子邮件匹配。

于 2013-10-04T14:55:04.197 回答
2

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

于 2013-10-01T21:26:43.823 回答
0

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

当您尝试模拟登录时,Google 始终提供 example@example.com 用户,它基本上发生在所有服务中,例如当您通过您的 Google 帐户在任何网站上提供登录时(例如使用 GWT 和 App Engine )。

如果您使用真实用户进行测试,或者您将 example@example.com 用户视为您的用户,那么您的站点会有什么不同?

于 2013-10-03T13:48:27.813 回答
0

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

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

然后在被调用的方法中,您将拥有来自 GAPI 的 User 对象。使用它从谷歌用户对象中获取实际电子邮件,如下所示

public myEndPointMethod( Foo foo, User user ){
    email = user.getEmail();
}
于 2013-10-03T19:16:17.637 回答
0

我用来自 UserFactory 的用户替换了 Oauth2 用户(example@example.com),它工作正常。我使用这种方法来验证所有 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 && "example@example.com".equalsIgnoreCase(user.getEmail()) ) {

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

    }
    return user;

}
于 2014-08-19T09:08:33.090 回答
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输入的开发服务器登录信息

于 2016-02-09T03:06:37.247 回答
-1

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

于 2015-05-05T12:40:25.500 回答