facebook / facebook-ruby-business-sdk

Ruby SDK for Meta Marketing API
https://developers.facebook.com/docs/business-sdk
Other
209 stars 160 forks source link

When successful in creating a custom conversion, the response body is incorrect and generates a JSON::ParserError #167

Open Mth0158 opened 2 years ago

Mth0158 commented 2 years ago

Which SDK version are you using?

0.12.0.1

What's the issue?

When successful in creating a custom conversion through the API, the response body is incorrect and generates a JSON::ParserError.

Steps/Sample code to reproduce the issue

First we generate a hash called facebook_attributes that we will use as params for our API call:

facebook_attributes = {
  name: "test_mth",
  description: "Tracks when something happens",
  rule: "{\"and\":[{\"event\":{\"eq\":\"PageView\"}},{\"or\":[{\"URL\":{\"i_contains\":\"some_url\"}}]}]}",
  event_source_id: our_facebook_pixel_id
}

Second we make a call to Facebook API:

custom_conversion = FacebookAds::AdAccount
  .get("act_#{our_facebook_ad_account_id}")
  .customconversions
  .create(facebook_attributes)

Observed Results:

We have the following error indicating the we try to parse something that is not parseable:

JSON::ParserError: 859: unexpected token at 'Sorry, this content isn't available right now'
from /Users/mathieu/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/json-2.6.1/lib/json/common.rb:216:in `parse'

Doing some research, it comes from the fact that we try to parse the response body which is equal to 'Sorry, this content isn't available right now', this causes a JSON::ParserError.

File responsible: lib/facebook_ads/api_response.rb Code responsible:

module FacebookAds
  class APIResponse
    attr_reader :status_code, :headers, :body

    def initialize(status_code, headers, body)
      @status_code = status_code
      @headers = headers
      @body = body
    end

    def result
      is_json_response? ? **JSON.parse(body)** : body
    end

    ...

  end
end

Expected Results:

It should: