Vlad
Vlad

Reputation: 3181

Verify app invite server-side

When our mobile app user sends app-invite to fb user and he accepts it, the server should give a reward to the first one. So I need a way to verify whether the invite was sent.

        var fb = new FacebookClient(APP_ID + "|" + SECRET_ID);
        fb.AppId = APP_ID;
        fb.AppSecret = SECRET_ID;
        dynamic result = fb.Get(???);

I searched on GraphAPI docs and it seems that I need to retrieve users apprequests. How to do that from the server side and where to look at to perform such verification?

UPDATE

Ok, now I know that it's allowed to reward only for accepted invites. I can record who invites who in the db and give a reward only when a new invited user joins. But I still need a way to verify that these invites were actually sent.

UPDATE2

As the documentation states apprequests call from application returns all the requests sent from this application. So I think it would be enough for me to just check that there are any requests from this app:

        dynamic result = fb.Get("/" + facebookId + "/apprequests");
        IEnumerable data = result.data;
        return data.Cast<object>().Count() != 0;

But I can't check it now. Can anyone confirm that if a user sends invite to app to another user this invite will be seen through apprequests from the application access token?

Upvotes: 1

Views: 184

Answers (2)

Vlad
Vlad

Reputation: 3181

Done it:

    public static bool CheckInvite(string fromId, string toId)
    {
        var fb = new FacebookClient(APP_ID + "|" + SECRET_ID);
        fb.AppId = APP_ID;
        fb.AppSecret = SECRET_ID;
        dynamic result = fb.Get(string.Format("/{0}/apprequests", toId));
        foreach (var el in result.data)
            if ((string)el.from.id == fromId)
            {
                DateTime dateTime = DateTime.Parse((string)el.created_time, CultureInfo.InvariantCulture);
                if ((DateTime.Now - dateTime).TotalMinutes < 15)
                {
                        return true;
                }
            }

        return false;
    }

Upvotes: 0

razon
razon

Reputation: 4050

my code for this:

        public static FacebookRequestData GetInviteHash()
        {
            string requestId = Request["request_ids"];
            var accessToken = GetAccessToken(ConfigurationManager.AppSettings["FacebookAppId"], ConfigurationManager.AppSettings["FacebookSecret"]);

            string response;
            using (var webClient = new WebClient())
            {
                response = webClient.DownloadString(string.Format("https://graph.facebook.com/{0}?{1}", requestId, accessToken));
            }

            var javaScriptSerializer = new JavaScriptSerializer();

            return javaScriptSerializer.Deserialize<FacebookRequestData>(javaScriptSerializer.Deserialize<FacebookRequestInfo>(response).data);
        }

        private static string GetAccessToken(string appId, string password)
        {
            using (var webClient = new WebClient())
            {
                return webClient.DownloadString(string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials", appId, password));
            }
        }

        private class FacebookRequestInfo
        {
            public string data { get; set; }
        }

FacebookRequestData - my custom class with structure of fields that I posted to fb earlier

Upvotes: 0

Related Questions