首页 文章

将我的“应用”验证为Google Cloud Endpoints而不是“用户”

提问于
浏览
8

我要做的是将我的Android应用验证到Google Cloud Endpoint . 基本上, endpoints 应该只允许我的Android应用程序访问方法,而不是其他任何东西 .

我做过这些事情 -

  • 在Google Cloud Console中使用Eclipse中的SHA1值创建客户端ID .

  • 在Google Cloud 端控制台中为我的 endpoints 项目创建一个Web客户端ID .

  • 在每个 endpoints 上提到的“@Api”中添加这两个客户端ID .

  • 在 endpoints 方法中添加额外的“user”参数 .

  • 将后端重新生成并部署到 Cloud 端 .

但是当我运行它时,“用户”总是变为“空” . 我正在努力寻找一种适当的工作方法来完成这一切 .

我搜索了很多论坛,但没有找到合适的答案 .

这是另一篇类似的帖子Restrict access to google cloud endpoints to Android app

这是我正在使用的参考 - https://developers.google.com/appengine/docs/java/endpoints/auth

以前有人在这做过吗?我的主要目标是出于明显的安全原因,不允许未经身份验证的应用程序和外部世界访问 endpoints . 我不想使用基于最终用户的身份验证,因为我想让我的应用程序非常简单 .

3 回答

  • 0

    听起来它正如预期的那样工作 . 您可以控制哪些客户端应用程序可以通过客户端ID调用您的 endpoints 方法 . User参数恰好为null,因为您没有进行最终用户身份验证 . User参数表示实际的真实用户(Google帐户) . 因此,如果您不需要最终用户身份验证方法,则只需不定义User参数,否则忽略null值 . 您说您的问题是User参数设置为null . 在这种情况下你有什么期望?

  • 0

    您需要在客户端上调用authenticate,然后您正在使用的库可能会“注入”用户信息 .

  • 0

    这对我有用:

    假设你有以下键:

    static final String WEB_CLIENT_ID = "somekeyfor webclientid.apps.googleusercontent.com";
    

    static final String ANDROID_CLIENT_ID =“somekeyfor androidclientid.apps.googleusercontent.com”; static final String ANDROID_AUDIENCE = WEB_CLIENT_ID;

    你的Api anotation应该是这样的:

    @Api(
            name = "yourapiname",
            clientIds = {CloudEndpoint.WEB_CLIENT_ID,CloudEndpoint.ANDROID_CLIENT_ID},
            audiences = {CloudEndpoint.ANDROID_AUDIENCE},
            version = "v1",
            namespace = @ApiNamespace(
                    ownerDomain = "myapp.app.com",
                    ownerName = "myapp.app.com",
                    packagePath = ""
            )
    )
    

    在下面的注释中,请注意您的受众是变量 - > ANDROID_AUDIENCE,它等于WEB_CLIENT_ID .

    现在在您的应用程序端,当您创建googleAccountCredential对象时,您应该传递Web客户端ID,如下所示:

    mAccountCredentials = GoogleAccountCredential.usingAudience(getApplicationContext(),"server:client_id:" + "yourwebclientID");
    

    请注意,即使正确完成此操作,如果您在设备中不存在mAccountCredentials.setSelectedAccountName(“accontname”)中传递的帐户名, endpoints 中的用户对象仍可能显示为Null . 因此,请确保您传递的帐户名称确实存在于Android设备中,方法是转到 - >(设置/帐户)

相关问题