0

我在 windows phone 中有一个应用程序并在 facebook 上注册,我将一些数据存储在一个表中,但是,一个用户在表中存储了不止一次。我尝试(获取 fb-id)检查表上是否有具有该 fb-id 的记录,但在重新注册之前,新用户应该检查是否存在,但是,例如异步方法没有顺序并且总是首先执行查询插入,因为我可以解决这个问题?

客户端(有限)

                    await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
                    Message = string.Format("User Authenticate - {0}", App.MobileService.CurrentUser.UserId);

                    //***** Get fb info
                    var userId = App.MobileService.CurrentUser.UserId;
                    var facebookId = userId.Substring(userId.IndexOf(':') + 1);
                    var client = new HttpClient();
                    var fbUser = await client.GetAsync("https://graph.facebook.com/" + facebookId);
                    var response = await fbUser.Content.ReadAsStringAsync();
                    var jo = JObject.Parse(response);
                    var FbidUser = jo.GetValue("id");
                    var userName = jo.GetValue("name");
                    var genero = jo.GetValue("gender");

但是,我应该在服务器客户端做,但是如何插入一次信息数据,我的意思是,在插入之前检查表中的记录。服务器端 Azure:

function insert(item, user, request) 
{
  item.UserName = "<unknown>"; // default
  var identities = user.getIdentities();
  var req = require('request');

  if (identities.facebook)
  {
    var fbAccessToken = identities.facebook.accessToken;
    var url = 'https://graph.facebook.com/me?access_token=' + fbAccessToken;

    req(url, function (err, resp, body)
    {
        if (err || resp.statusCode !== 200)
        {
            console.error('Error sending data to FB Graph API: ', err);
            request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
        }
        else 
        {
            try 
            {
                var userData = JSON.parse(body);
                item.UserName = userData.name;
                request.execute();
            } catch (ex) 
            {
                console.error('Error parsing response from FB Graph API: ', ex);
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
            }
        }
    });

  }
}

根据上面的代码,我有第二个问题,在 facebook 的开发者中心,我可以访问电子邮件和照片(我的应用程序),我猜userData变量包含这些信息?,我如何访问它?,如何调用那些属性的其余信息在哪里?

4

1 回答 1

2

如果您只想阻止插入同名用户的两个项目,最简单的方法是根本不使用名称,而是使用 FB id(毕竟,两个不同的人可能拥有一样的名字)。您可以使用以下脚本执行此操作:

function insert(item, user, request) 
{
    item.UserId = user.userId;
    var currentTable = tables.current;
    currentTable.where({ UserId: user.userId }).read({
        success: function(results) {
            if (results.length > 0) {
                // an item with that user id already exists in the table
                request.respond(400, 
                    { error: 'item already in the table' });
            } else {
                // new user, can insert it here
                request.execute();
            }
        }
    });
}

现在,如果您真的想使用用户作为表的“键”,您也可以执行类似的操作:

function insert(item, user, request) 
{
  item.UserName = "<unknown>"; // default
  var identities = user.getIdentities();
  var req = require('request');
  var currentTable = tables.current;

  if (identities.facebook)
  {
    var fbAccessToken = identities.facebook.accessToken;
    var url = 'https://graph.facebook.com/me?access_token=' + fbAccessToken;

    req(url, function (err, resp, body)
    {
        if (err || resp.statusCode !== 200)
        {
            console.error('Error sending data to FB Graph API: ', err);
            request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
        } else {
            try {
                var userData = JSON.parse(body);
                item.UserName = userData.name;
                currentTable.where({ UserName: item.UserName }).read({
                    success: function(results) {
                        if (results.length > 0) {
                            request.respond(statusCodes.BAD_REQUEST,
                                { error: 'Name already in the table' });
                        } else {
                            request.execute();
                        }
                    }
                });
            } catch (ex) {
                console.error('Error parsing response from FB Graph API: ', ex);
                request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
            }
        }
    });

  }
}
于 2013-08-15T22:26:17.330 回答