facebook / facebook-ruby-business-sdk

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

How to get 'X-FB-Ads-Insights-Throttle' header from response? #48

Closed joaquincorrales closed 4 years ago

joaquincorrales commented 6 years ago

Trying to create a scheduled job to fetch insights from customer ad accounts but I need to constantly check the 'X-FB-Ads-Insights-Throttle' header in order to make sure Facebook doesn't limit me.

How can I check this header when calling the insights calls using the library?

joephayes commented 6 years ago

Check the last_api_response of the ad object you are using. You can access the headers from there. See https://github.com/facebook/facebook-ruby-business-sdk/issues/50#issuecomment-405943862 for an example.

joaquincorrales commented 6 years ago

Joe thanks a lot. I'm also trying to handle an ad account being limited or app being limited and catching it with a begin -> rescue but the last_api_response can't be found in the rescue. Maybe I should just make sure it never happens as opposed to trying to handle when it does

joaquincorrales commented 6 years ago

I'm already performing some sort of "ping" to the ad account before performing anything so I might be good but you can never be too safe.

There's always the scenario that when you ping the adaccount everything is fine but halfway through processing they reach their limit you know

joephayes commented 6 years ago

This allows access to the headers of the request that caused the error:

begin
  ad_account = FacebookAds::AdAccount.get("act_#{ad_account_id}", 'name')

  puts "Ad Account Name: #{ad_account.name}"
rescue FacebookAds::APIError => e
  puts e.instance_variable_get(:@api_response).headers
end

produces this:

{"access-control-allow-origin"=>"*", "strict-transport-security"=>"max-age=15552000; preload", "x-fb-trace-id"=>"XXXXXX", "content-type"=>"application/json; charset=UTF-8", "x-fb-rev"=>"4116048", "x-app-usage"=>"{\"call_count\":0,\"total_cputime\":0,\"total_time\":0}", "cache-control"=>"no-store", "pragma"=>"no-cache", "expires"=>"Sat, 01 Jan 2000 00:00:00 GMT", "www-authenticate"=>"OAuth \"Facebook Platform\" \"invalid_token\" \"Invalid OAuth access token.\"", "x-fb-debug"=>"XXXXX", "date"=>"Wed, 18 Jul 2018 22:22:19 GMT", "connection"=>"close", "content-length"=>"113"}
joaquincorrales commented 6 years ago

That helps tremendously thank you so much Joe!

One last question. Do you know if there's an option to not allow users to edit campaigns made through the API in their ads managers? Or is that impossible.

joephayes commented 6 years ago

You are welcome! Unfortunately, I don't know if there is a way to block users from editing API created campaigns with Ads Manager. Sorry!

stale[bot] commented 4 years ago

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.