使用Parse.com在Facebook注册时设置用户的电子邮件

时间:2015-01-13 23:52:39

标签: facebook-graph-api parse-platform

我正在使用Parse.com并尝试设置用户注册Facebook。

首次使用Facebook进行身份验证时,会在_User上调用beforeSave以获取其他用户详细信息:

function UserBeforeSave(request, response){
  var user = request.object,
      auth = user.get('authData');

  // check if user is newly registered
  if (!user.existed()) {

    // Check if a user signs up with facebook
    if (Parse.FacebookUtils.isLinked(request.object)) {

      // Query Graph API for user details
      Parse.Cloud.httpRequest({
        url:'https://graph.facebook.com/v2.2/me?access_token=' + auth.facebook.access_token,
        success:function(httpResponse){

          // Map facebook data to user object
          if (httpResponse.data.first_name) request.object.set('first_name', httpResponse.data.first_name);
          if (httpResponse.data.last_name)  request.object.set('last_name',  httpResponse.data.last_name);
          if (httpResponse.data.email)      request.object.set('email',      httpResponse.data.email);

          response.success();
        },
        error:function(httpResponse){
          console.error(httpResponse);
          response.error();
        }
      });
    } else {
      response.success();
    }
  } else {
    response.success();
  }

}

问题是该电子邮件行实际上是在破坏操作时出错:

Can't modify email in the before save trigger

我尝试将此代码移动到afterSave但是authdata不可用,因此很难调用FB API。因此,该电子邮件目前处于空白状态。

我认为这是将Parse与Facebook API集成的一个非常常见的用例,我是否在集成过程中遗漏了一些自动提取电子邮件的内容?

1 个答案:

答案 0 :(得分:0)

我只是在客户端进行图形查询并在那里设置电子邮件。这很好。

您是否有理由在用户的前/后保存中执行此操作?