facebook / facebook-python-business-sdk

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

How to upload XML file to Facebook with the facebook-python-ads-sdk? #153

Closed FX-HAO closed 8 years ago

FX-HAO commented 8 years ago

I've seen this post Product Feed Uploads.And i want to know whether this is possbile with facebook-python-ads-sdk?

I've tried the following codes:

with open('product_feed_upload.xml', 'rb') as f:

    params = {
        'file': f
    }

    data = FacebookAdsApi.get_default_api().call(
        'POST',
        'https://graph.facebook.com/v2.5/12345678/uploads',
        params=params,
        api_version='v2.5',
    ).json()['data']

But i got the following error:

  Status:  400
  Response:
    {
      "error": {
        "message": "(#100) Invalid file. Expected file of one of the following types: text/csv, text/plain, text/xml, application/xml, application/rss+xml, application/atom+xml", 
        "code": 100, 
        "type": "OAuthException", 
        "fbtrace_id": "FtqCsz3N0V0"
      }
    }
ccquiel commented 8 years ago

Same issue, but with a csv file. Did you figured out what's going on?

FX-HAO commented 8 years ago

@ccquiel Sorry, I don't figure it out even now :( Did you try directly upload the file with Javascript? I just uploaded it successfully by Postman. And please let me know if you work it out.

ccquiel commented 8 years ago

@FX-HAO Thanks for replying! How did you upload it with Postman? Did you send the whole file as in your first post? I'm trying the following with the requests library:


import requests
url = "https://graph.facebook.com/v2.5/<product_feed_id>/uploads"
params = {
    "access_token": my_access_token,
    "file": file
}
r = requests.post(url, params=params)

After opening the file (f = open("feed.xml", 'rb')), I've sent:

file = 'feed.xml'
file = f.read()
file = base64.b64encode(f.read())
file = base64.b64encode(f.read()).decode("utf8")

and none of them work, I keep getting:

Exception: (#100) Invalid file. Expected file of one of the following types: text/csv, text/plain...

:(

edwinlunando commented 8 years ago

getting the same error here. Any update?

rituparnamukherjee commented 8 years ago

Can you try using the new productfeed.py object to upload. You can do so by using the create_upload method.

maheshwar-grofers commented 8 years ago

for me this worked:

curl -X POST \
https://graph.facebook.com/v2.7//uploads \ -F "file=@test.csv;type=text/csv" \ -F "access_token=EAAQBBkFQPAMBAF2gFh2KxzBeRGgDgSyJ0b0XZBJHDysVSYM7Sp0Fhh1gZCSSBZAODKR8fhnfEeAtZCXCkWKY3TS5kuIwrZAS8hZCc5GFgeCfDtbeYUpP1h5KykrsJmxZCJQYms7sOYTTvV2tm60ee0H3zu03aODBgijZBWtaLZBFz5AZDZD"