如何在 Rails 上使用 Ruby 从 Facebook 营销 API 获得潜在影响 5
How to get Potential Reach from Facebook Marketing API using Ruby on Rails 5
我正在尝试获取基于 Python 的 potential reach from the Facebook Marketing API using Ruby on Rails 5. I found the Ruby SDK here but I can't see the sample codes nor the functions that I need for getting the data that I want. I also found this post,并尝试将语言转换为 Ruby,这让我得到了这段代码:
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。
我也尝试模拟示例 cURL 命令并提出以下测试脚本:
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 和访问令牌。就藏在这里吧。所以上面的代码现在 returns 出现以下错误:
{
"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_ 但结果相同。我现在遇到了死胡同,所以我想就如何做到这一点提出一些建议。非常感谢。
原来是因为post的要求。我应该改用 GET 请求。这是我的工作代码,带有针对目标规范的附加 interest 参数。
def get_target(interest_id, interest_name)
account_id = @account_id
access_token = @access_token
url = "https://graph.facebook.com/v2.10/act_#{account_id}/reachestimate"
params = {
"access_token" => access_token,
"targeting_spec" => "{" +
"\"geo_locations\": {\"country_groups\":[\"Worldwide\"]}," +
" \"age_min\": 20, \"age_max\": 40," +
" \"interests\": [" +
"{\"id\":#{interest_id}, \"name\":\"#{interest_name}\"}" +
"]" +
"}"
}
response = get_response(url, params)
response
end
def get_response(url, params)
uri = URI.parse(url)
uri.query = URI.encode_www_form(params)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri)
response = http.request(request)
response.body
end
我正在尝试获取基于 Python 的 potential reach from the Facebook Marketing API using Ruby on Rails 5. I found the Ruby SDK here but I can't see the sample codes nor the functions that I need for getting the data that I want. I also found this post,并尝试将语言转换为 Ruby,这让我得到了这段代码:
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。
我也尝试模拟示例 cURL 命令并提出以下测试脚本:
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 和访问令牌。就藏在这里吧。所以上面的代码现在 returns 出现以下错误:
{
"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_ 但结果相同。我现在遇到了死胡同,所以我想就如何做到这一点提出一些建议。非常感谢。
原来是因为post的要求。我应该改用 GET 请求。这是我的工作代码,带有针对目标规范的附加 interest 参数。
def get_target(interest_id, interest_name)
account_id = @account_id
access_token = @access_token
url = "https://graph.facebook.com/v2.10/act_#{account_id}/reachestimate"
params = {
"access_token" => access_token,
"targeting_spec" => "{" +
"\"geo_locations\": {\"country_groups\":[\"Worldwide\"]}," +
" \"age_min\": 20, \"age_max\": 40," +
" \"interests\": [" +
"{\"id\":#{interest_id}, \"name\":\"#{interest_name}\"}" +
"]" +
"}"
}
response = get_response(url, params)
response
end
def get_response(url, params)
uri = URI.parse(url)
uri.query = URI.encode_www_form(params)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri)
response = http.request(request)
response.body
end