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 Invalid Parameter OAuthException on trying to upload assets #680

Closed navaneethkwalee closed 3 months ago

navaneethkwalee commented 3 months ago

Code:

from facebook_business import FacebookAdsApi
from facebook_business.adobjects.adimage import AdImage

# from facebookads.adobjects.adimage import AdImage

import requests
import os

def test_sdk():
    FacebookAdsApi.init(
        access_token=ACCESS_TOKEN,
        app_id=facebook_settings.APP_ID,
        app_secret=APP_SECRET,
        account_id=AD_ACCOUNT_ID,
        debug=True,
    )

    image = AdImage(parent_id=PARENT_ID)
    image[AdImage.Field.filename] = IMAGE_PATH
    image.remote_create()

    # Output image Hash
    print('hash:', image[AdImage.Field.hash])

Error:

  Status:  400
  Response:
    {
      "error": {
        "message": "Invalid parameter",
        "type": "OAuthException",
        "code": 100,
        "error_subcode": 1487197,
        "is_transient": false,
        "error_user_title": "Error processing image",
        "error_user_msg": "There was a problem processing the image for the ad.",
        "fbtrace_id": "AoO92B1CaLGuaA1Wnr6BZop"
      }
    }
navaneethkwalee commented 3 months ago

Was referring to https://developers.facebook.com/docs/marketing-api/reference/ad-image/ and it's outdated but got succeeded after looking at the SDK codebase:

import base64

file_path = "test1080.jpg"
with open(file_path, "rb") as image_file:
    img_data = base64.b64encode(image_file.read()).decode('utf-8')

FacebookAdsApi.init(APP_ID, APP_SECRET, ACCESS_TOKEN, debug=True)
my_account = AdAccount(AD_ACCOUNT_ID)
resp = my_account.create_ad_image(
    params={
        'bytes': img_data,
    }
)
print(resp)

I'm not find the documentation of this anywhere, please update it if it's the latest one.

stcheng commented 3 months ago

thank you. we'll update the doc accordingly.