简体   繁体   English

验证我的“应用”到Google Cloud Endpoints而非“用户”的身份

[英]Authenticate my “app” to Google Cloud Endpoints not a “user”

What I'm trying to do is to authenticate my Android app to the Google Cloud Endpoint. 我想做的是向Google Cloud Endpoint验证我的Android应用程序。 Basically the endpoints should only allow my Android app to access the methods and nothing else. 基本上,端点仅应允许我的Android应用访问方法,而不能进行其他操作。

I have done these things - 我已经做了这些事情-

  1. Create a client id using my SHA1 value in Eclipse in the Google Cloud Console. 使用我在Google Cloud Console中的Eclipse中的SHA1值创建客户端ID。

  2. Create a web client id in the Google Cloud Console for my endpoint project. 在Google Cloud Console中为我的终端项目创建一个Web客户端ID。

  3. Add both these client id's in the "@Api" mentioned on each endpoint. 在每个端点上提到的“ @Api”中添加这两个客户端ID。

  4. Add an extra "user" parameter in the endpoint methods. 在端点方法中添加一个额外的“用户”参数。

  5. Regenerate and deploy the backend to the cloud. 重新生成后端并将其部署到云中。

But when I'm running this the "user" is always coming as "null". 但是,当我运行它时,“用户”总是以“空”出现。 I'm at my wits end trying to find a proper working method for doing all this. 我竭尽全力试图找到合适的工作方法来完成所有这一切。

I've searched many forums but no proper answers anywhere. 我搜索了很多论坛,但是在任何地方都没有正确的答案。

Here's another similar post Restrict access to google cloud endpoints to Android app 这是另一篇类似的文章, 将Google Cloud端点的访问权限限制为Android应用

This is the reference I'm using - https://developers.google.com/appengine/docs/java/endpoints/auth 这是我正在使用的参考-https: //developers.google.com/appengine/docs/java/endpoints/auth

Has anyone here done this before? 有人在这里做过吗? My main goal is to not allow unauthenticated apps and outside world to access the endpoints, for obvious security reasons. 我的主要目标是出于明显的安全原因,不允许未经身份验证的应用程序和外界访问终结点。 I don't want to use end-user based authentication since I want to keep my app very simple. 我不想使用基于最终用户的身份验证,因为我想让我的应用程序非常简单。

It sounds like it's working as intended. 听起来像按预期工作。 You control which client apps can call your endpoint methods via the client IDs as you have already done. 您已经控制了哪些客户端应用可以通过客户端ID调用端点方法。 The User parameter is coming in as null precisely because you aren't doing end-user authentication. 正是因为您没有进行最终用户身份验证,所以User参数以null形式出现。 The User parameter represents an actual real user (Google Account). User参数代表实际的实际用户(Google帐户)。 So if you don't need end-user authenticated methods, you can just simply not define the User parameter, or else ignore the null value. 因此,如果不需要最终用户身份验证的方法,则可以简单地不定义User参数,或者忽略null值。 You said your problem is that the User parameter is set null. 您说的问题是User参数设置为null。 What are you expecting it to be in this scenario? 您期望在这种情况下会发生什么?

您需要在客户端上调用身份验证,然后您正在使用的库可能会“注入”用户信息。

Here's what worked for me : 这是对我有用的东西:

Let's say you have the keys below : 假设您有以下按键:

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

static final String ANDROID_CLIENT_ID = "somekeyfor androidclientid.apps.googleusercontent.com"; 静态最终字符串ANDROID_CLIENT_ID =“ somekeyfor androidclientid.apps.googleusercontent.com”; static final String ANDROID_AUDIENCE = WEB_CLIENT_ID; 静态最终字符串ANDROID_AUDIENCE = WEB_CLIENT_ID;

Your Api anotation should look like this : 您的Api注释应如下所示:

@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 = ""
        )
)

In the annotation below, notice how your audience is the variable --> ANDROID_AUDIENCE which is equal to WEB_CLIENT_ID. 在下面的注释中,请注意您的听众如何成为变量-> ANDROID_AUDIENCE,该变量等于WEB_CLIENT_ID。

Now in your app side, when you create the googleAccountCredential object, you should pass in the Web Client Id like this : 现在,在您的应用程序端,当您创建googleAccountCredential对象时,您应该像这样传递Web客户端ID:

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

Note that even if this is properly done, your user object in the endpoint might still coming out as Null if the account name you pass in mAccountCredentials.setSelectedAccountName("accontname") does not exist in the device. 请注意,即使正确完成此操作,如果您在mAccountCredentials.setSelectedAccountName(“ accontname”)中传递的帐户名在设备中不存在,则端点中的用户对象仍可能显示为Null。 Therefore make sure the account name you pass does exist in the Android device by going to --> (Settings/Accounts) 因此,请转到->(设置/帐户),确保您传递的帐户名确实存在于Android设备中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何精确设置clientId和受众群体以对Android应用程序的Google Cloud Endpoints进行身份验证 - How exactly to set clientIds and audiences to authenticate Google Cloud Endpoints for an Android App Google Cloud Endpoints和用户身份验证 - Google Cloud Endpoints and user's authentication Google App Engine Cloud Endpoints userId为null - Google App Engine Cloud Endpoints userId is null 将对Google云端点的访问权限限制为Android应用 - Restrict access to google cloud endpoints to Android app 将 Firebase 应用与 Google Sign In 结合使用时,如何使用我的 Google Cloud Function 进行身份验证? - How do I authenticate with my Google Cloud Function when using my Firebase App with Google Sign In? 如何在谷歌云 shell 中验证 firebase 用户 - How to authenticate firebase user in google cloud shell 如何从服务器端(Google App Engine,Cloud Endpoints)向我的客户端发送信息? - How can I send from the server-side (Google App Engine, Cloud Endpoints) the information to my client? 如何将Facebook登录和电子邮件注册添加到我的Google Cloud Endpoints App(Java)? - How do I add both Facebook Login and Email registration to my Google Cloud Endpoints App (Java)? Android客户端-使用Google Cloud Endpoints的Google App引擎身份验证 - Android Client - Google App engine authentication using Google Cloud Endpoints 对Google App Engine和Google Cloud端点感到困惑 - Confused about Google App Engine and Google Cloud Endpoints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM