gpt4 book ai didi

java - 如何将 Facebook 登录和电子邮件注册添加到我的 Google Cloud Endpoints 应用程序 (Java)?

转载 作者:可可西里 更新时间:2023-10-31 22:05:06 24 4
gpt4 key购买 nike

所以我有一个使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com