- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
所以我有一个使用 Google App Engine 和 Google Cloud Endpoints 作为 Java 后端的应用程序。我目前正在处理用户身份验证,这是我正在尝试做的事情:
当用户首次打开应用程序时,他们可以选择“通过 Facebook 登录”或使用他们的电子邮件地址注册。然后,这些数据将存储在用户对象中,注册后会将他们定向到应用程序主页。它将保存在他们的首选项中,这样他们就不需要在每次打开应用程序时都登录(如果有的话)。
现在我听说您可以为 Facebook 使用自定义身份 validator ,但是没有太多关于此的文档。如何使用 Google Cloud Endpoint 的身份 validator 实现电子邮件注册和 Facebook 登录选项?还是我应该采取不同的方法?
谢谢。
最佳答案
我的方法是使用Facebook 登录 方法(Facebook SDK for Android)。 Facebook 身份验证过程(成功时)返回一个对象,我可以从中获取用户的电子邮件,然后使用 Datastore API 将其保存在我的 Endpoints 类中。为了检查用户是否已经登录,我选择了 SharedPreferences 方法和 GSON 库将对象解析为 JSON 字符串并将它们保存在首选项中。
下面的链接和我的示例代码:
Regarding the Authenticator I found this SO answer
More info about Facebook login method
Saving custom objects in SharedPreferences
通过 Facebook 身份验证获取用户的电子邮件
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
if (isSessionCalled == false) {
Log.i(TAG, "Logged in...");
System.out.println("Token=" + session.getAccessToken());
new Request(
session,
"/me",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
if (response != null) {
GraphObject object = response.getGraphObject();
String email = (String) object.getProperty("email");
Log.i(TAG, "user email : " + email);
String firstName = (String) object.getProperty("first_name");
String lastName = (String) object.getProperty("last_name");
mUserTask = new UserAsyncTask();
mUserTask.execute(email);
}
}
}
).executeAsync();
isSessionCalled = true;
}
else {
Log.w(TAG, "session called twice");
}
}
else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
将用户存储在我的后端:
@ApiMethod(name = "storeUserModel")
public UserModel storeUserModel(UserModel userModel) throws UserAlreadyExistsException, UserNotFoundException {
logger.info("inside storeUser");
String email = userModel.getEmail();
UserModel checkUser = getUserModel(email);
logger.info("after getUserModel with email " + email);
if (checkUser == null) {
logger.info("inside checkUser is NULL");
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastoreService.beginTransaction();
try {
Entity userEntity = new Entity(UserModel.class.getSimpleName(), email);
userEntity.setProperty("nickname", userModel.getNickname());
// TODO save the pheromones with the key of userEntity
datastoreService.put(userEntity);
txn.commit();
storePheromoneList(userModel.getPheromoneList(), userEntity.getKey(), datastoreService);
} finally {
if (txn.isActive()) {
logger.severe("rolled back with email : " + email);
txn.rollback();
}
}
}
else {
throw new UserAlreadyExistsException();
}
return userModel;
}
触发调用我的后端的类
public class EndpointsServer implements Server {
private static final String TAG = "EndpointsServer";
final UserModelApi userEndpointsApi;
public EndpointsServer() {
UserModelApi.Builder builder = new UserModelApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
userEndpointsApi = builder.build();
}
@Override
public User getUser(String email) {
User user = null;
try {
Log.d(TAG, "in getUser with email " +email);
// get user from db
UserModel userModel = userEndpointsApi.getUserModel(email).execute();
if (userModel != null) {
Log.d(TAG, "user != null with email " + email);
user = new User(userModel);
}
} catch (IOException e) {
e.printStackTrace();
}
return user;
}
}
在成功登录时存储用户:
String userString = gson.toJson(user, User.class);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(USER_KEY, userString);
editor.commit();
它还有更多功能,例如另一个客户端类,用于构建对后端的 api 调用和许多其他细节。如果你愿意,我可以发布它。
关于java - 如何将 Facebook 登录和电子邮件注册添加到我的 Google Cloud Endpoints 应用程序 (Java)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28980637/
我正在使用 winsock 和 C++ 来设置服务器应用程序。我遇到的问题是对 listen 的调用会导致第一次机会异常。我想通常这些可以忽略(?),但我发现其他人也有同样的问题,它导致应用程序偶尔挂
我对 Wireguard 的理解是服务器和客户端的接口(interface)(虽然看起来听不清?)每个都有自己的 .conf 文件。例如,考虑以下 .conf 文件。 [Interface] Priv
如何将多个实体从客户端传递到 Google Cloud Endpoint? 例如,传递单个实体很容易在服务器的 Endpoint api 源文件中完成: public class SomeEndpoi
我试图建立一个路径来将文件从一个目录复制到另一个目录。但不是使用:从(文件://源目录)到(文件://目标目录)我想做这样的事情: from(direct:start) .to(direct:do
我有一个非常小的网站,在 Azure 上以共享托管配置运行。我上面有一堆涉及打开套接字的网络代码,所有这些代码都成功运行。 我编写了一些发送 ping 的代码,但抛出了 PingException,内
我试图了解如何将 Cloud Endpoints 与自定义身份验证结合使用。从文档中我了解到它从 securityDefinitions 开始: securityDefinitions: yo
我在理解有关此文档的过程中遇到了一些麻烦。位于 ... https://developers.google.com/appengine/docs/java/endpoints/consume_js 具
我一直在尝试在生成的 Endpoint 类中创建一些新方法,但发现了这种奇怪的行为:我可以向生成的类添加一个方法,但我无法添加其中两个方法,无论我选择这两个方法中的哪一个添加。这是我生成的类的代码,我
azure 中的“输入端点”和“内部端点”是什么?如何创建新的“输入端点”? &如何将数据发送到“输入端点”? 输入端点端口 65400 端口是什么? 最佳答案 输入端点是云服务和 Internet
首先,对您可能犯的语法错误表示歉意。我的英语不是很好。 我是 Spring 新手,我正在尝试创建基本身份验证安全性。 我正在尝试配置一个端点具有公共(public)访问权限,而其他端点则具有用户访问权
我试图让图标部分包含我自己的图标,而不是通过尝试猴子补丁 ApiConfigGenerator.get_descriptor_defaults 来谷歌搜索图标。不幸的是,当发现文档完成时,这些将被忽略
我正在尝试跟随初学者到 WCF 页面上的演示视频 MSDN . 第一个视频或多或少地工作得很好。我现在接近第二个视频的结尾。我使用的是 VS2010/.NET 4.0,而视频似乎使用的是 VS2008
这个问题完全来自我在这里问过(并得到回答)的相关问题:Error when trying to retrieve a single entity 据我了解,要使用已提供的辅助方法以外的属性(例如 'i
WSL1 没有问题。我想升级到 WSL 2。 当我尝试升级到 wsl2 时,命令行失败。我试图删除 Ubuntu 并重新安装它,没有区别。 虚拟机平台处于事件状态。 Windows 内部版本号:190
我有一个代理lambda函数的AWS api。我目前使用不同的端点和单独的 lambda 函数: api.com/getData --> getData api.com/addData --> add
我正在构建一个 Chrome 应用,我真的希望它能够通过云端点与我的服务器进行通信,但有两个问题我不确定如何克服: Google apis javascript 库是一个外部脚本,我无法在 Chrom
我正在我的 gke 集群上运行 kubernetes 1.9.4 我有两个 pod,gate,它正在尝试连接到 coolapp,它们都是用 elixir 编写的 我正在使用libcluster连接我的
阅读Where to place the Asynctask in the Application和 http://android-developers.blogspot.com/2009/05/pa
我不清楚 @Named 在 Google Cloud Endpoints 中的用途。文档说: This annotation indicates the name of the parameter i
我正在关注 Getting Started guide对于使用 Maven 的 Java 中的 Google Cloud Endpoints,我无法使用 API Explorer 访问我的端点。 尽管
我是一名优秀的程序员,十分优秀!