没有Google帐户的Google Cloud端点

Tim*_*ens 12 authentication google-app-engine google-cloud-endpoints

我们的网络应用程序不提供Google帐户身份验证.我们使用WebApp2身份验证实现了自己的身份验证:http://webapp-improved.appspot.com/tutorials/auth.html .

我们希望将Cloud Endpoints用作移动应用/第三方开发人员的API,但我们仍然希望使用oAuth2进行身份验证.

实现这个步骤需要哪些步骤?我们是否需要在AppEngine上设置我们自己的oAuth服务器并且Google客户端库是否兼容?

Bog*_*scu 1

你不需要做任何事。我在应用程序引擎上有一个联合登录应用程序,我最近在其中添加了一个使用 Cloud Endpoints 的 Android 应用程序。您不必执行任何特殊操作,只需将 User 参数放入您的函数即可。在用户对象中,您将找到必须授权才能访问数据的用户电子邮件。

@Api(name = "my_api",
        version = "v1",
        scopes = {"https://www.googleapis.com/auth/userinfo.email"},
        clientIds = {Constants.AUTH_CLIENT,
                Constants.AUTH_CLIENT_APIEXPLORER})
public class MyEndpoint {
    @ApiMethod(name = "fistEndpoint")
    public ResponseObject fistEndpoint(User user) throws OAuthRequestException {
        if (user == null) {
            throw new OAuthRequestException("Access denied!");
        }
        String email = user.getEmail();
        //Authorize the request here
        //make the ResponseObject and return it
    }
}
Run Code Online (Sandbox Code Playgroud)

创建端点后,访问: https://your-app.appspot.com/_ah/api/explorer并测试它

更新: 上面的示例仅限于 Google 帐户。如果您想要不同类型的帐户,您可以查看这篇文章: Custom Authentication for Google Cloud Endpoints (instead of OAuth2)