skip to Main Content

I am trying to get user hometown and location using Facebook graph API for the logged-in user

Public permissions to fetch location and hometown is granted by the user.

Graph API version v2.8

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

2

Answers


  1. You need to ask for permissions: user_hometown and user_location. The hometown/location is not included in the public permissions.

    You can also check the scope of your access token here

    Login or Signup to reply.
  2. Here is how you can get the complete user profile using FB token:

    I am assuming you have given permission before getting token from graph api explore which is:

    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;

    Hope it helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search