Exemple #1
0
 def __init__(
         self,
         app_id=None,
         app_secret=None,
         short_token=None,
         long_term_token=None,
         application_only_auth=False,
         initial_access_token=True,  # type: bool
         version=None,
         timeout=None,
         sleep_on_rate_limit=False,
         sleep_seconds_mapping=None,
         proxies=None,
         debug_http=False):
     BaseApi.__init__(self,
                      app_id=app_id,
                      app_secret=app_secret,
                      short_token=short_token,
                      long_term_token=long_term_token,
                      application_only_auth=application_only_auth,
                      initial_access_token=initial_access_token,
                      version=version,
                      timeout=timeout,
                      sleep_on_rate_limit=sleep_on_rate_limit,
                      sleep_seconds_mapping=sleep_seconds_mapping,
                      proxies=proxies,
                      debug_http=debug_http)
Exemple #2
0
    def __init__(self,
                 app_id=None,
                 app_secret=None,
                 short_token=None,
                 long_term_token=None,
                 instagram_business_id=None,
                 version=None,
                 timeout=None,
                 sleep_on_rate_limit=False,
                 sleep_seconds_mapping=None,
                 proxies=None,
                 debug_http=False):
        BaseApi.__init__(self,
                         app_id=app_id,
                         app_secret=app_secret,
                         short_token=short_token,
                         long_term_token=long_term_token,
                         version=version,
                         timeout=timeout,
                         sleep_on_rate_limit=sleep_on_rate_limit,
                         sleep_seconds_mapping=sleep_seconds_mapping,
                         proxies=proxies,
                         debug_http=debug_http)

        self.instagram_business_id = instagram_business_id
Exemple #3
0
    def testGetAppToken(self):
        api = BaseApi(app_id="id", app_secret="secret", long_term_token="token")
        with responses.RequestsMock() as m:
            m.add("GET", self.ACCESS_TOKEN_URL, json=self.APP_ACCESS_TOKEN)

            token = api.get_app_token(return_json=True)

            self.assertEqual(token["access_token"], "123456789|fvYq7ORmqKa2IDCijArPOYKB0")
Exemple #4
0
    def testGetLongToken(self):
        api = BaseApi(app_id="id", app_secret="secret", long_term_token="token")

        with responses.RequestsMock() as m:
            m.add("GET", self.ACCESS_TOKEN_URL, json=self.LONG_TERM_TOKEN)

            token = api.get_long_token(short_token="short-lived token", return_json=True)

            self.assertEqual(token["access_token"], "token")
Exemple #5
0
    def testExchangeAccessToken(self):
        api = BaseApi(app_id="appId", app_secret="appSecret", long_term_token="token")
        _, state = api.get_authorization_url()

        response = "https://localhost/?code=code&state=PyFacebook#_=_"
        with responses.RequestsMock() as m:
            m.add("POST", api.exchange_access_token_url, json=self.LONG_TERM_TOKEN)

            r1 = api.exchange_access_token(response=response, return_json=True)
            self.assertEqual(r1["access_token"], "token")

            r2 = api.exchange_access_token(response=response)
            self.assertEqual(r2.access_token, "token")
            self.assertEqual(r2.token_type, "bearer")
Exemple #6
0
    def testApiVersion(self):
        with self.assertRaises(pyfacebook.PyFacebookException):
            BaseApi(version="1.0")

        with self.assertRaises(pyfacebook.PyFacebookException):
            BaseApi(version="v1.0")

        with self.assertRaises(pyfacebook.PyFacebookException):
            BaseApi(version="version")

        api = BaseApi(long_term_token="token", version=None)
        self.assertEqual(api.version, api.VALID_API_VERSIONS[-1])

        api = BaseApi(long_term_token="token", version="3.3")
        self.assertEqual(api.version, "v3.3")
Exemple #7
0
    def testApiToken(self, patched_time_sleep):
        with self.assertRaises(pyfacebook.PyFacebookException):
            BaseApi()

        api = BaseApi(long_term_token="token", debug_http=True)
        self.assertEqual(api._access_token, "token")

        with responses.RequestsMock() as m:
            m.add("GET", self.ACCESS_TOKEN_URL, json=self.APP_ACCESS_TOKEN)
            api = BaseApi(app_id="123456789", app_secret="secret", application_only_auth=True, sleep_on_rate_limit=True)
            self.assertEqual(api._access_token, "123456789|fvYq7ORmqKa2IDCijArPOYKB0")

        with responses.RequestsMock() as m:
            m.add("GET", self.ACCESS_TOKEN_URL, json=self.LONG_TERM_TOKEN)
            api = BaseApi(app_id="123456789", app_secret="secret", short_token="short-lived token")
            self.assertEqual(api._access_token, "token")
Exemple #8
0
    def testBuildSleepResource(self):
        d = {10: 3, 20: 5, 50: 20}
        api = BaseApi(long_term_token="token", sleep_on_rate_limit=True, sleep_seconds_mapping=d)
        self.assertEqual(len(api.sleep_seconds_mapping), 3)
        self.assertEqual(api.sleep_seconds_mapping[0].percent, 10)
        self.assertEqual(api.rate_limit.get_sleep_seconds(sleep_data=api.sleep_seconds_mapping), 3)

        # test max sleep
        headers = {'x-app-usage': '{"call_count":120,"total_cputime":15,"total_time":12}'}
        api.rate_limit.set_limit(headers)
        self.assertEqual(api.rate_limit.get_sleep_seconds(sleep_data=api.sleep_seconds_mapping), 60 * 10)
Exemple #9
0
    def testGetAuthorizationUrl(self):
        with self.assertRaises(pyfacebook.PyFacebookException):
            api = BaseApi(long_term_token="token")
            api.get_authorization_url()

        api = BaseApi(app_id="appId", app_secret="appSecret", long_term_token="token")

        url, state = api.get_authorization_url()
        self.assertEqual(state, "PyFacebook")
        self.assertTrue(url)
Exemple #10
0
    def testRequest(self):
        api = BaseApi(app_id="12345678", app_secret="secret", long_term_token="token")
        with responses.RequestsMock() as m:
            m.add("GET", "https://graph.facebook.com/", json={"message": "message"})

            r = api._request(
                path="", args=None
            )
            r.json()["message"] = "message"

        with responses.RequestsMock() as m:
            m.add("POST", "https://graph.facebook.com/", json={"message": "message"})

            r = api._request(
                path="", method="POST", args=None, post_args={"auth": 1},
            )
            r.json()["message"] = "message"

        with responses.RequestsMock() as m:
            m.add("GET", "https://graph.facebook.com/", body=HTTPError("..."))

            with self.assertRaises(pyfacebook.PyFacebookException):
                api._request(
                    path="",
                )
Exemple #11
0
 def __init__(self,
              app_id=None,
              app_secret=None,
              short_token=None,
              long_term_token=None,
              application_only_auth=False,
              version=None,
              timeout=None,
              sleep_on_rate_limit=False,
              proxies=None,
              debug_http=False):
     BaseApi.__init__(self,
                      app_id=app_id,
                      app_secret=app_secret,
                      short_token=short_token,
                      long_term_token=long_term_token,
                      application_only_auth=application_only_auth,
                      version=version,
                      timeout=timeout,
                      sleep_on_rate_limit=sleep_on_rate_limit,
                      proxies=proxies,
                      debug_http=debug_http)
 def __init__(
         self,
         app_id=None,
         app_secret=None,
         short_token=None,
         long_term_token=None,
         initial_access_token=True,
         timeout=None,  # type: Optional[int]
         sleep_on_rate_limit=False,  # type: bool
         sleep_seconds_mapping=None,  # type: Optional[Dict]
         proxies=None,  # type: Optional[dict]
         debug_http=False  # type: bool
 ):
     BaseApi.__init__(self,
                      app_id=app_id,
                      app_secret=app_secret,
                      short_token=short_token,
                      long_term_token=long_term_token,
                      timeout=timeout,
                      initial_access_token=initial_access_token,
                      sleep_on_rate_limit=sleep_on_rate_limit,
                      sleep_seconds_mapping=sleep_seconds_mapping,
                      proxies=proxies,
                      debug_http=debug_http)
Exemple #13
0
    def testGetTokenInfo(self):
        debug_token_utl = "https://graph.facebook.com/{}/debug_token".format(BaseApi.VALID_API_VERSIONS[-1])
        with responses.RequestsMock() as m:
            m.add("GET", debug_token_utl, json=self.TOKEN_INFO)

            api = BaseApi(long_term_token="long-live token")
            info = api.get_token_info(return_json=True)
            self.assertEqual(info["type"], "USER")

            api = BaseApi(app_id="appId", app_secret="Secret", long_term_token="long-live token")
            info = api.get_token_info(input_token="NeedToken")
            self.assertEqual(info.is_valid, True)
Exemple #14
0
 def testCheckError(self):
     data = {"error": {"code": 100}}
     api = BaseApi(long_term_token="token")
     with self.assertRaises(pyfacebook.PyFacebookException):
         api._check_graph_error(data)