1
我想從使用Ruby on Rails 5的Facebook營銷API中獲取potential reach。我找到了Ruby SDK here,但我看不到示例代碼和我需要獲取我想要的數據的功能。我也發現了這個post這是基於Python的,並試圖將語言轉化爲紅寶石這使我這個代碼:如何使用Ruby on Rails從Facebook營銷API獲得潛在覆蓋面5
require "facebook_ads"
class FacebookApi
attr_reader :ad_instance
def initialize(opts={})
@ad_instance = FacebookAds::AdAccount.get("act_#{opts[:account_id]}")
raise 'Account ID Key must be provided' unless opts[:account_id]
end
def get_target()
targeting_spec = {
'geo_locations': {
'countries': ['US'],
},
'age_min': 20,
'age_max': 40,
}
params = {
'targeting_spec': targeting_spec,
}
@ad_instance.get_reach_estimate(params: params)
end
end
然而,功能get_reach_estimate沒有找到。
我也試着模仿樣本捲曲的命令,而不是與下面的測試腳本上來:
require 'net/http'
require 'uri'
require "erb"
include ERB::Util
class FacebookApi
def get_target()
account_id = <ACCOUNT_ID>
access_token = <USER_ACCESS_TOKEN_WITH_ADS_MANAGEMENT_PRIVILEGE>
uri = URI.parse("https://graph.facebook.com/v2.10/act_#{account_id}/reachestimate")
request = Net::HTTP::Post.new(uri)
request.set_form_data(
"access_token" => access_token,
"targeting_spec" => url_encode("{\"geo_locations\": {\"countries\":[\"US\"]}, \"age_min\": 20, \"age_max\": 40}")
)
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts response.code
puts response.body
end
end
請留意,我用我的代碼中的實際帳戶ID和訪問令牌。只是把它藏在這裏。所以,現在上面的代碼返回以下錯誤:
{
"error": {
"message": "Unsupported post request. Object with ID <ACCOUNT_ID> does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api",
"type": "GraphMethodException",
"code": 100,
"fbtrace_id": "ESJks9/KxJC"
}
}
我敢肯定,我加入了ad_management訪問我的訪問令牌。我也嘗試刪除URI中的前綴act_,但結果相同。我現在正處於死路一條,所以我想問一些關於如何做到這一點的建議。非常感謝你。