无法从Facebook图形API获取用户家乡和位置

时间:2017-04-12 07:26:06

标签: facebook facebook-graph-api

我正在尝试为登录用户使用Facebook图形API获取用户hometownlocation

用户授予获取位置和家乡的公共权限。

图表API版本 v2.8

/me?fields=name,picture,location,hometown

2 个答案:

答案 0 :(得分:1)

您需要申请权限:user_hometownuser_location。家乡/地点不包含在公共权限中。

您还可以检查访问令牌的范围here

答案 1 :(得分:0)

以下是使用FB令牌获取完整的用户个人资料的方法:

我假设您已从图api浏览器获取令牌之前获得了许可,这是:

https://developers.facebook.com/tools/explorer/

public Profile GetProfile(string facebookID)
        {
            var url = "?fields=first_name,last_name,name,age_range,birthday, education, gender, hometown, locale, location, third_party_id, timezone, email";

            dynamic result = _facebookClient.Get(facebookID + url);

            var profile = new Profile();

            if (result != null)
            {
                string resultStringify = result.ToString();
                var resultBlock = JObject.Parse(resultStringify);
             
                //if (result.ToString().Contains("first_name")) {
                //    profile.First_Name = resultBlock["first_name"].ToString();
                //}
                profile.First_Name = resultStringify.ExtractIfValueIsAvailable("first_name", resultBlock);
                //if (result.ToString().Contains("last_name")) {
                //    profile.Last_Name = resultBlock["last_name"].ToString();
                //}
                profile.Last_Name = resultStringify.ExtractIfValueIsAvailable("last_name", resultBlock);
                //if (result.ToString().Contains("name")) {
                //    profile.Name = resultBlock["name"].ToString();
                //}
                profile.Name = resultStringify.ExtractIfValueIsAvailable("name", resultBlock);

                if (result.ToString().Contains("age_range"))
                {
                    if (result.age_range.min != null) profile.Age_Range_Min = result.age_range.min;
                    if (result.age_range.max != null) profile.Age_Range_Max = result.age_range.max;
                }

                //if (result.ToString().Contains("birthday")) {
                //    profile.Birthday = DateTime.ParseExact(result.birthday, "MM/dd/yyyy", CultureInfo.InvariantCulture);
                //}
                profile.Birthday = resultStringify.ExtractIfDateTime("birthday", resultBlock);
                //if (result.ToString().Contains("gender")) {
                //    profile.Gender = resultBlock["gender"].ToString();
                //}
                profile.Gender = resultStringify.ExtractIfValueIsAvailable("gender", resultBlock);
                //if (result.ToString().Contains("hometown")) {
                //    profile.Hometown = resultBlock["hometown"]["name"].ToString();
                //}
                profile.Hometown = resultStringify.ExtractIfNameValueIsAvailable("hometown", resultBlock);
                //if (result.ToString().Contains("locale")) {
                //    profile.Locale = resultBlock["locale"].ToString();
                //}
                profile.Locale = resultStringify.ExtractIfValueIsAvailable("locale", resultBlock);
                //if (result.ToString().Contains("location")) {
                //    profile.Location = resultBlock["location"]["name"].ToString();
                //}
                profile.Location = resultStringify.ExtractIfNameValueIsAvailable("location", resultBlock);
                //if (result.ToString().Contains("third_party_id")) {
                //    profile.Third_Party_ID = resultBlock["third_party_id"].ToString();
                //}

                profile.Third_Party_ID = resultStringify.ExtractIfValueIsAvailable("third_party_id", resultBlock);

                profile.Timezone = resultStringify.ExtractIfValueIsAvailable("timezone", resultBlock);

                profile.EMail = resultStringify.ExtractIfValueIsAvailable("email", resultBlock);

            }

           
            return profile;
        }

public static string ExtractIfValueIsAvailable(this string result, string fieldName, dynamic resultBlock) => result.Contains(fieldName) ? resultBlock[fieldName].ToString() : string.Empty;

希望有帮助。