Exemple #1
0
from facebook_sdk.exceptions import FacebookResponseException
from facebook_sdk.facebook import Facebook

facebook = Facebook(
    app_id='{app_id}',
    app_secret='{app_secret}',
)

facebook.set_default_access_token(access_token='{access_token}')

try:
    response = facebook.post(
        endpoint='/me/photos',
        params={
            'message': 'My awesome photo upload example.',
            'source': facebook.file_to_upload('file/to/upload.jpg'),
        },
    )
except FacebookResponseException as e:
    print e.message
else:
    print response.json_body
class TestFacebook(TestCase):
    def setUp(self):
        super(TestFacebook, self).setUp()
        self.facebook = Facebook(
            app_id='123',
            app_secret='secret',
            default_access_token='my_token',
            default_graph_version='v2.6',
        )

    def test_initialize(self):
        self.assertIsInstance(self.facebook.app, FacebookApp)
        self.assertIsInstance(self.facebook.client, FacebookClient)
        self.assertIsInstance(self.facebook.oauth_client, OAuth2Client)

        self.assertEqual(self.facebook.app.id, '123')
        self.assertEqual(self.facebook.app.secret, 'secret')
        self.assertEqual(self.facebook.default_graph_version, 'v2.6')
        self.assertEqual(str(self.facebook.default_access_token), 'my_token')

    def test_initialize_required_app_id_config(self):
        self.assertRaises(
            FacebookSDKException,
            Facebook,
        )

    def test_initialize_required_app_secret_config(self):
        self.assertRaises(
            FacebookSDKException,
            Facebook,
            app_id='123',
        )

    def test_initialize_invalid_access_token(self):
        self.assertRaises(
            ValueError,
            Facebook,
            app_id='123',
            app_secret='123',
            default_access_token=12345,  # token int not supported
        )

    def test_send_request(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='',
        ))
        response = self.facebook.send_request(method='GET',
                                              endpoint='some_endpoint',
                                              access_token='foo_token',
                                              graph_version='v2.7')
        self.assertIsInstance(response, FacebookResponse)
        self.assertEqual(response.request.method, 'GET')
        self.assertEqual(response.request.endpoint, 'some_endpoint')
        self.assertEqual(response.request.access_token, 'foo_token')
        self.assertEqual(response.request.graph_version, 'v2.7')
        self.assertDictEqual(response.request.params,
                             {'access_token': 'foo_token'})

    def test_send_request_default_access_token_and_version(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='',
        ))
        response = self.facebook.send_request(
            method='GET',
            endpoint='some_endpoint',
        )

        self.assertEqual(response.request.method, 'GET')
        self.assertEqual(response.request.endpoint, 'some_endpoint')
        self.assertEqual(response.request.access_token, 'my_token')
        self.assertEqual(response.request.graph_version, 'v2.6')
        self.assertDictEqual(response.request.params,
                             {'access_token': 'my_token'})

    def test_send_batch_request(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='[{"code":"200","body":"Foo"}]',
        ))
        batches = [
            self.facebook.request(
                method='GET',
                endpoint='some_endpoint',
            )
        ]

        response = self.facebook.send_batch_request(requests=batches,
                                                    access_token='foo_token',
                                                    graph_version='v2.7')

        self.assertEqual(response.request.method, 'POST')
        self.assertEqual(response.request.access_token, 'foo_token')
        self.assertEqual(response.request.graph_version, 'v2.7')
        self.assertDictEqual(response.request.params,
                             {'access_token': 'foo_token'})

    def test_send_batch_request_default_access_token_and_version(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='[{"code":"200","body":"Foo"}]',
        ))
        batches = [
            self.facebook.request(
                method='GET',
                endpoint='some_endpoint',
            )
        ]

        response = self.facebook.send_batch_request(requests=batches, )

        self.assertEqual(response.request.method, 'POST')
        self.assertEqual(response.request.access_token, 'my_token')
        self.assertEqual(response.request.graph_version, 'v2.6')
        self.assertDictEqual(response.request.params,
                             {'access_token': 'my_token'})

    def test_get_facebook_get_method(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='',
        ))
        response = self.facebook.get(endpoint='/test', )
        self.assertEqual(response.request.method, 'GET')

    def test_post_facebook_get_method(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='',
        ))
        response = self.facebook.post(endpoint='/test', )
        self.assertEqual(response.request.method, 'POST')

    def test_delete_facebook_get_method(self):
        self.facebook.client = FakeFacebookClient(fake_response=FakeResponse(
            status_code=200,
            content='',
        ))
        response = self.facebook.delete(endpoint='/test', )
        self.assertEqual(response.request.method, 'DELETE')

    def test_filet_to_upload(self):
        facebook_file = self.facebook.file_to_upload(
            path='{base_path}/foo.txt'.format(
                base_path=os.path.dirname(os.path.abspath(__file__))))

        self.assertIsInstance(facebook_file, FacebookFile)
from facebook_sdk.facebook import Facebook

facebook = Facebook(
    app_id='{app_id}',
    app_secret='{app_secret}',
)

facebook.set_default_access_token(access_token='{access_token}')

batch = {
    'photo-one':
    facebook.request(
        endpoint='/me/photos',
        params={
            'message': 'Foo photo.',
            'source': facebook.file_to_upload('path/to/foo.jpg'),
        },
    ),
    'photo-two':
    facebook.request(
        endpoint='/me/photos',
        params={
            'message': 'Bar photo.',
            'source': facebook.file_to_upload('path/to/bar.jpg'),
        },
    ),
    'photo-three':
    facebook.request(
        endpoint='/me/photos',
        params={
            'message': 'Other photo.',