【发布时间】:2017-11-02 19:26:03
【问题描述】:
我是 Google App Engine 的新手,我正在尝试制作我的第一个引擎并将其连接到我的 Android 应用程序。我已经浏览了本教程以了解它:
https://cloud.google.com/endpoints/docs/frameworks/legacy/v1/java/helloendpoints-android-studio
我让它工作正常。我可以从我的 android 应用程序访问我的应用程序引擎,并获得所需的响应。问题是,我想将我的 API 的端点仅限于我的应用程序的用户。
这是我的 API 方法(来自教程),就目前而言,每个人都可以访问我的 api 资源管理器并在其中执行方法,只要他们登录到任何 Google 帐户即可。
我希望用户只能从我的应用程序中执行此方法。
这是我的应用引擎 java 文件:
package com.example.Barda.myapplication.backend;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.UnauthorizedException;
import com.google.appengine.api.users.User;
import javax.inject.Named;
/**
* An endpoint class we are exposing
*/
@Api(
name = "myApi",
version = "v1",
clientIds = {Constants.ANDROID_CLIENT_ID},
audiences="firebase-wiki-race.appspot.com",
namespace = @ApiNamespace(
ownerDomain = "backend.myapplication.Barda.example.com",
ownerName = "backend.myapplication.Barda.example.com",
packagePath = ""
)
)
public class MyEndpoint {
/**
* A simple endpoint method that takes a name and says Hi back
*/
@ApiMethod(name = "sayHi")
public MyBean sayHi(@Named("name") String name) throws UnauthorizedException {
// if (user == null) throw new UnauthorizedException("User is Not Valid");
MyBean response = new MyBean();
response.setData("Hi, " + name);
return response;
}
}
这是常量类:
package com.example.Barda.myapplication.backend;
/**
* Contains the client IDs and scopes for allowed clients consuming your API.
*/
public class Constants {
public static final String ANDROID_CLIENT_ID = "*********************.apps.googleusercontent.com";
}
我使用我的应用程序的 SH-1 和包名称 ANDROID_CLIENT_ID 生成。
我在网上搜索了很多,也阅读了博客和主题,但我无法让它发挥作用。这是可能的事情吗?我究竟做错了什么?
【问题讨论】:
标签: android api google-app-engine google-cloud-platform