如何在devserver上使用Oauth测试Cloud Endpoints

时间:2013-10-01 13:37:32

标签: google-app-engine google-cloud-endpoints

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

我的问题是,在本地devserver上,我的用户用户始终设置为 example@example.com ,即使我已经完成了通常的身份验证,访问代码等等,有一个有效的authed用户。

我得到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)。

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

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

7 个答案:

答案 0 :(得分:4)

似乎无法完成。我最终通过将以下代码放在我的Endpoint方法的顶部来编写它。

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

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

答案 1 :(得分:2)

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

答案 2 :(得分:0)

问题在于,当您在本地进行身份验证时,您不会通过Google服务器进行身份验证,因此对您的用户进行身份验证实际上并非在本地进行身份验证。

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

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

答案 3 :(得分:0)

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

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

然后在被调用的方法中,您将拥有GAPI中的User对象。 使用此选项可以从Google用户对象中获取实际的电子邮件,例如

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

答案 4 :(得分: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;

}

答案 5 :(得分: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

输入的开发服务器登录信息

答案 6 :(得分:-1)

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