facebook / facebook-python-business-sdk

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

API Error or Library Error, Specifying file type when creating block list draft create_block_list_draft #626

Closed rsinghrandhawa-ias closed 1 year ago

rsinghrandhawa-ias commented 1 year ago

I receive either an API Error when using the library for create_block_list_draft or an error in the code when trying to specify the file type

import logging
from facebook_business.api import FacebookAdsApi
from facebook_business.api import FacebookSession
from facebook_business.adobjects.business import Business

def main():
    BUSINESS_ID = "<obfuscated>"
    access_token = "<obfuscated>"
    app_secret = "<obfuscated>"
    config = {'app_id': BUSINESS_ID, 'app_secret': app_secret, 'access_token':access_token}
    session = FacebookSession(
        config['app_id'],
        config['app_secret'],
        config['access_token'],
        debug=True
    )
    FacebookAdsApi.set_default_api(FacebookAdsApi(session))
    fb_business = Business(fbid=BUSINESS_ID)
    filepath="testfile.csv"
    params = {
       'publisher_urls_file': filepath
    }
    response = fb_business.create_block_list_draft(params=params)
    print(response)

if __name__ == "__main__":
    logging.basicConfig(
        level=logging.INFO, format="%(levelname)s (%(name)s:%(lineno)s): %(message)s"
    )
    main()

I receive the following error

"message": "(#100) Param publisher_urls_file must be a file with one of the following types: text/csv, text/plain, application/vnd.ms-excel. Received file of type ''.",

So I tried a variation of that with

params = {
    'publisher_urls_file':(filepath, "text/csv")
}

And received an error in the library

response = fb_business.create_block_list_draft(params=params) File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/adobjects/business.py", line 802, in create_block_list_draft request.add_params(params) File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/api.py", line 656, in add_params self.add_param(key, params[key]) File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/api.py", line 642, in add_param if not self._param_checker.is_valid_pair(key, value): File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/typechecker.py", line 81, in is_valid_pair return self.is_type(value_type, value) File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/typechecker.py", line 92, in is_type ... TypeError: stat: path should be string, bytes, os.PathLike or integer, not tuple

I also tried

in_file = open(filepath, "rb")
data = in_file.read() 
in_file.close()
params = {'publisher_urls_file': data}

and received this error compatible. Expect file; got <class 'bytes'> warnings.warn(message) Traceback (most recent call last): File "/home/etl-pm-pipeline-facebook/services/brandsafety/upload_blocklists/upload_blocklists-2.py", line 44, in main() File "/home/etl-pm-pipeline-facebook/services/brandsafety/upload_blocklists/upload_blocklists-2.py", line 37, in main response = fb_business.create_block_list_draft(params=params) File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/adobjects/business.py", line 812, in create_block_list_draft return request.execute() File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/api.py", line 681, in execute with open_files(self._file_params) as files: File "/usr/local/Cellar/python@3.6/3.6.15/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 81, in enter return next(self.gen) File "/home/etl-pm-pipeline-facebook/venv/lib/python3.6/site-packages/facebook_business/api.py", line 882, in open_files opened_files.update({key: open(path, 'rb')}) OSError: [Errno 63] File name too long: b'...

rsinghrandhawa-ias commented 1 year ago

I raised a ticket on https://developers.facebook.com/support/bugs/ but the support person did not assist at all

rsinghrandhawa-ias commented 1 year ago

resolved found a different way to update block list