Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for retrieving user permissions #264

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aa30f81
retrieve permissions object from API
seawolf42 Jan 12, 2016
2e7d5cd
transform result into dictionary for convenient lookups
seawolf42 Jan 12, 2016
fb0ca41
retrieve the token from the environment once and make all tests depen…
seawolf42 Jan 12, 2016
f3aa306
move test to bottom of file
seawolf42 Jan 12, 2016
e0485ff
flake8 compliant
seawolf42 Jan 13, 2016
a1b8d7c
compliance with python 2.6
seawolf42 Jan 13, 2016
efb65e2
use unittest2 if older python to support skipIf decorator
seawolf42 Jan 13, 2016
fc4ed3d
Merge branch 'master' of github.com:mobolic/facebook-sdk
seawolf42 Aug 14, 2016
d2d7486
use user ID for any arbitrary user rather than just the current user
seawolf42 Aug 15, 2016
17291f9
if user does not exist or has not authorized the app, raise GraphAPIE…
seawolf42 Aug 15, 2016
65f3d00
lint clean
seawolf42 Aug 15, 2016
2d94d2f
documentation for get_permissions method
seawolf42 Aug 15, 2016
2917c49
better grammar
seawolf42 Aug 15, 2016
e78968b
remove file accidentally checked in
seawolf42 Aug 18, 2016
52c73e5
Merge remote-tracking branch 'upstream/master'
seawolf42 Nov 24, 2016
2223169
remove support for python 2.6
seawolf42 Nov 24, 2016
b1d8876
flake8 compliance
seawolf42 Nov 26, 2016
e1a05e8
bug fix
seawolf42 Nov 26, 2016
ca15bf8
remove **args parameter
seawolf42 Jan 6, 2017
8ffedb9
change to string format instead of concatenation
seawolf42 Jan 6, 2017
a69a778
Merge remote-tracking branch 'upstream/master'
seawolf42 Jan 6, 2017
3c8911f
create test users rather than using hard-coded user ID
seawolf42 Jan 7, 2017
c422097
add args to request
seawolf42 Jan 7, 2017
b9e5e61
working unit tests
seawolf42 Jan 7, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,28 @@ Generates Facebook login URL to request access token and permissions.
**Example**

.. code-block:: python

app_id = 1231241241
canvas_url = 'https://domain.com/that-handles-auth-response/'
perms = ['manage_pages','publish_pages']
fb_login_url = graph.auth_url(app_id, canvas_url, perms)
print(fb_login_url)

get_permissions
^^^^^^^^^^^^^

https://developers.facebook.com/docs/graph-api/reference/user/permissions/

Returns the permissions granted to the app by the user with the given ID as a
``dict``.

**Parameters**

* ``user_id`` - A ``string`` or an ``int`` that is a unique ID for a user.

**Example**

.. code-block:: python

permissions = graph.get_permissions(user_id=12345)
print(permissions["public_profile"])
9 changes: 9 additions & 0 deletions facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def __init__(self, access_token=None, timeout=None, version=None,
else:
self.version = "v" + default_version

def get_permissions(self, user_id):
"""Fetches the permissions object from the graph."""
response = self.request(
"{0}/{1}/permissions".format(self.version, user_id), {}
)["data"]
result = dict((x["permission"], x["status"] == "granted")
for x in response)
return result

def get_object(self, id, **args):
"""Fetches the given object from the graph."""
return self.request("{0}/{1}".format(self.version, id), args)
Expand Down
27 changes: 27 additions & 0 deletions test/test_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,32 @@ def test_request_access_tokens_are_unique_to_instances(self):
self.assertEqual(graph2.request.__defaults__[0], None)


class TestGetUserPermissions(FacebookTestCase):
"""
Test if user permissions are retrieved correctly.

Note that this only tests if the returned JSON object exists and is
structured as expected, not whether any specific scope is included
(other than the default `public_profile` scope).

"""
def test_get_user_permissions_node(self):
token = facebook.GraphAPI().get_app_access_token(
self.app_id, self.secret)
graph = facebook.GraphAPI(access_token=token)
self.create_test_users(self.app_id, graph, 1)
permissions = graph.get_permissions(self.test_users[0]['id'])
self.assertIsNotNone(permissions)
self.assertEqual(permissions['public_profile'], True)
self.assertEqual(permissions['user_friends'], True)
self.assertFalse('email' in permissions)

def test_get_user_permissions_nonexistant_user(self):
token = facebook.GraphAPI().get_app_access_token(
self.app_id, self.secret)
with self.assertRaises(facebook.GraphAPIError):
facebook.GraphAPI(token).get_permissions(1)


if __name__ == '__main__':
unittest.main()