facebook / facebook-python-business-sdk

Python SDK for Meta Marketing APIs
https://developers.facebook.com/docs/business-sdk
Other
1.32k stars 645 forks source link

Getting UnicodeDecodeError from curlify module when debug=True on video upload #681

Open navaneethkwalee opened 3 months ago

navaneethkwalee commented 3 months ago

code:

  FacebookAdsApi.init(
      access_token=facebook_settings.ACCESS_TOKEN,
      app_id=facebook_settings.APP_ID,
      app_secret=facebook_settings.APP_SECRET,
      debug=True,
  )
  response = ad_account.create_ad_video(
      params={
          'source': 'test-video.mp4',
      },
  )

  asset_id = response[AdVideo.Field.id]
  print(asset_id)

error:

  File "/Users/navaneethsuresh/virtual_envs/test/lib/python3.11/site-packages/facebook_business/api.py", line 669, in execute
    response = self._api.call(
               ^^^^^^^^^^^^^^^
  File "/Users/navaneethsuresh/virtual_envs/test/lib/python3.11/site-packages/facebook_business/api.py", line 321, in call
    print(curlify.to_curl(response.request))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/navaneethsuresh/virtual_envs/test/lib/python3.11/site-packages/curlify.py", line 28, in to_curl
    body = body.decode('utf-8')
           ^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaf in position 163: invalid start byte
sys:1: ResourceWarning: unclosed file <_io.BufferedReader name='/Users/navaneethsuresh/code/test/test-video.mp4'>

The network functionality works fine when I set debug=False.

Probably need to keep this inside a try except as a quickfix in api.py

.......
        else:
            response = self._session.requests.request(
                method,
                path,
                data=params,
                headers=headers,
                files=files,
                timeout=self._session.timeout
            )
        if self._enable_debug_logger:
            import curlify
            print(curlify.to_curl(response.request))
        fb_response = FacebookResponse(
            body=response.text,
            headers=response.headers,
            http_status=response.status_code,
            call={
                'method': method,
                'path': path,
                'params': params,
                'headers': headers,
                'files': files,
            },
        )

        if fb_response.is_failure():
            raise fb_response.error()

        self._num_requests_succeeded += 1
        return fb_response