我正在使用 Android Studio 为 Android 应用程序生成 Google Cloud Endpoints。我已经部署了端点,它们工作正常。
我想要实现的是只允许我的应用程序访问端点。所有文档都讨论如何为用户启用身份验证,但我只需要限制只能从我的应用程序访问端点。我不想使用用户凭据,因为 api 不使用它。
我尝试的是在 Google 控制台中为我的应用程序创建一个客户端 ID 并添加到 API 注释中
@Api(name = "messageEndpoint", clientIds = {Ids.WEB_CLIENT_ID, Ids.ANDROID_CLIENT_ID, Ids.API_EXPLORER}, audiences = {Ids.ANDROID_AUDIENCE}}
并且对于每个 API 方法,我都添加了一个 User 参数并检查用户是否为 null。我的理解是,如果从客户端 IDS 之一调用 API,则用户不应为 null。但用户始终为空。
我错过了什么吗?
在我的 Android 应用中有
GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(DelegateActivity.this, "server:client_id:" + Ids.ANDROID_AUDIENCE);
MessageEndpoint.Builder endpointBuilder = new MessageEndpoint.Builder(
AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
credential);
我得到的异常是:
java.lang.NullPointerException: [qi] accountName cannot be null.
E at android.os.Parcel.readException(Parcel.java:1431)
E at android.os.Parcel.readException(Parcel.java:1379)
E at com.google.android.gms.internal.a$a$a.a(Unknown Source)
E at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
E at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
E at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:277)
E at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:301)
E at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:854)
E at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
E at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
E at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
最佳答案
我的理解是 user 在您描述的情况下将为 null:验证应用程序而不是用户。
如果应用身份验证失败,则您的端点甚至不会被调用。
我假设字符串“[qi] accountName cannot be null.”来自您的应用程序?
关于android - 将对谷歌云端点的访问限制为 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17873993/