Search code examples
djangofacebookfacebook-graph-apifandjango

Fandjango Facebook user only returns a few Facebook fields


I am using Fandjango and trying to access the Facebook user's relationship_status and significant_other.name, but it can't find those attributes. My code:

from django.shortcuts import render
from quiz.models import Quiz, Question
from django.http import HttpResponse
from fandjango.decorators import facebook_authorization_required

@facebook_authorization_required(permissions=['user_activities', 'user_hometown', 'user_interests', 'user_location', 'user_relationship_details', 'user_events', 'friends_relationships'])
def home(request):
    user = request.facebook.user
    print dir(user)


    context = {
    'user': user,           
    }

return render(request, 'quiz/home.html', context)

I've been printing out a list of all the Facebook attributes from https://developers.facebook.com/docs/graph-api/reference/user/ to keep track of what works and what doesn't:

<ul>
    <li>ID: {{user.id}}</li>
    <li>Link: {{user.link}}</li>
    <li>First Name: {{user.first_name}}</li>
    <li>Quotes: {{user.quotes}}</li>
    <li>Name: {{user.name}}</li>
    <li>Hometown: {{user.hometown}}</li>
    <li>Bio: {{user.bio}}</li>
    <li>Religion: {{user.religion}}</li>
    <li>Middle Name: {{user.middle_name}}</li>
    <li>About: {{user.about}}</li>
    <li>Verified: {{user.is_verified}}</li>
    <li>Gender: {{user.gender}}</li>
    <li>Third-party: {{user.third_party_id}}</li>
    <li>Relationship-status: {{user.relationship_status}}</li>
    <li>Last-name: {{user.last_name}}</li>
    <li>Locale: {{user.locale}}</li>
    <li>Verified: {{user.verified}}</li>
    <li>Political: {{user.political}}</li>
    <li>Name-format: {{user.name_format}}</li>
    <li>Sig Other: {{user.significant_other}}</li>
    <li>Website: {{user.website}}</li>
    <li>Location: {{user.location}}</li>
    <li>Username: {{user.username}}</li>
</ul>

This only yields ID, Middle Name, Gender, Last-name, and locale. It seems pretty clear I'm only getting the public profile attributes, but don't get how to access the rest.

To figure out what attributes I have access to, I ran that dir(user) command and that yields:

['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', u'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_base_manager', '_default_manager', '_deferred', '_do_insert', '_do_update', '_get_FIELD_display', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_oauth_token_cache', '_perform_date_checks', '_perform_unique_checks', '_save_parents', '_save_table', '_set_pk_val', '_state', 'answer_set', 'authorized', 'birthday', 'clean', 'clean_fields', 'created_at', 'date_error_message', 'delete', 'email', 'extra_data', 'facebook_id', 'facebook_username', 'first_name', 'full_clean', 'full_name', 'gender', 'get_next_by_created_at', 'get_next_by_last_seen_at', 'get_previous_by_created_at', 'get_previous_by_last_seen_at', 'graph', 'id', 'last_name', 'last_seen_at', 'locale', 'middle_name', 'oauth_token', 'oauth_token_id', 'objects', 'permissions', 'picture', 'pk', 'prepare_database_save', 'save', 'save_base', 'serializable_value', 'synchronize', 'unique_error_message', 'validate_unique']

Obviously that shows the ones that are working, and doesn't list the ones that aren't.

I feel like I'm missing something pretty obvious about accessing non-public profile attributes but really don't know what it is.

EDIT: After doing more research I'm starting to think I need to pass the user's auth_token to the graph API to get all the attributes I want, but not sure how to do so in the Fandjango framework. This is my first try, but it returns "Graph API object is not callable":

oauth = request.facebook.user.oauth_token
user = request.facebook.user
facebook_graph = user.graph(oauth)

Solution

  • Hope you're not pulling your hair out too much about this :). To get any attributes that aren't part of the user's public profile you need to call:

    user = request.facebook.user.graph.get('me')
    

    The user object will now have the attributes you have permissions to use from the @facebook_authorization_required(permissions=[<your_permissions_here>] decorator.