facebook / facebook-python-business-sdk Goto Github PK
View Code? Open in Web Editor NEWPython SDK for Meta Marketing APIs
Home Page: https://developers.facebook.com/docs/business-sdk
License: Other
Python SDK for Meta Marketing APIs
Home Page: https://developers.facebook.com/docs/business-sdk
License: Other
I am not sure if batching will help with my problem, but I need to start from the beginning of a Campaign and process all Ad Group Stats per day. After processing 1-2 months I hit the rate limit. I would like to try batching to see if that would help with this issue.
There isn't an example of batching and using remote_read().
I know what I need to do: https://developers.facebook.com/docs/reference/ads-api/batch-requests/#adstatistics
The URL displayed in the executing command shell is
"https://grap.facebook.com/v2.4/ [id of adaccount]/customaudiences"
while in the actual Graph Explorer on the FB website it is
"https://grap.facebook.com/v2.4/ act_ [id of adaccount]/customaudiences"
I get a "Call was not successful"/ "Unsupported get request" message, Status 400.
Detailed here:
https://developers.facebook.com/docs/marketing-api/optimization_simplification
My question is, when is thew SDK going to begin supporting the new optimization fields, and is there any plan to support both types of optimization during the transition?
Thanks!
I am getting a warning I have never seen before when creating an audience in SDK version 2.3.1, Python version 2.7.9:
InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
After reading through the link, it appears that this is related to an incompatibility with urllib3 and Python 2.7.
Is there actually a security hole here or is this just an overly-cautious warning?
Assume s,t are AdGroup objects with valid id's already set and that b is a FacebookAdsApiBatch object.
Without using batch:
s.remote_read(fields=[AdGroup.Field.name, AdGroup.Field.campaign_id])
t.remote_read(fields=[AdGroup.Field.name, AdGroup.Field.campaign_id])
both s,t now contains the fields name and campaign_id, however if the call is made in this way instead:
s.remote_read(fields=[AdGroup.Field.name, AdGroup.Field.campaign_id], batch=b)
t.remote_read(fields=[AdGroup.Field.name, AdGroup.Field.campaign_id], batch=b)
b.execute()
The name and campaign_id fields are not set, the response bodies from the server only has the 'id' value set.
Backtrace:
File ".../facebookads/api.py", line 117, in error
self.body()
File ".../facebookads/exceptions.py", line 99, in __init__
KeyError: 'path'
Looks like the error is located here. The request_context
, i.e. error._call
has no key "path".
I got this on version 2.3.3
and using a batch query. Looks like this https://github.com/facebook/facebook-python-ads-sdk/blob/2.4.2/facebookads/api.py#L387-L407
is the problem (I don't see a path
key)
According to this https://github.com/facebook/facebook-python-ads-sdk/tree/master/examples/docs , it's possible to generate documentation through a tool called "DocSmith". Unfortunately, the link (https://our.intern.facebook.com/intern/wiki/Solutions_Engineering/Devsite_Dynamic_Doc_Code_Blocks) doesn't seem to point at anything anymore. Would love to see what DocSmith does since googling "Facebook DocSmith" gives me a list of people named "Doc Smith".
I know this probably connects to the Graph API for Ads data. But I was wondering if there are plans to allow to connecting to endpoints like these:
https://developers.facebook.com/docs/graph-api/reference/v2.2
Is there any chance you think it would be a good idea to make the use of appsecret_proof optional? It fails for me every time I try to create a session with it. Obviously works fine if I remove it.
https://github.com/facebook/facebook-python-ads-sdk/blob/master/facebookads/session.py#L66
Thank you,
Freddy
according to official announcement.
https://developers.facebook.com/ads/blog/post/2015/07/08/marketing-api-v2_4/
$ python -m facebookads.test.integration
.EEEEE..E
======================================================================
ERROR: runTest (__main__.AdCampaignTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 269, in runTest
self.assert_can_delete(self.subject)
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 192, in assert_can_delete
subject.remote_delete()
File "facebookads/objects.py", line 709, in remote_delete
self.clear_id()
File "facebookads/objects.py", line 448, in clear_id
del self[self.__class__.Field.id]
File "facebookads/objects.py", line 319, in __delitem__
del self._changes[key]
KeyError: 'id'
======================================================================
ERROR: runTest (__main__.AdGroupTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 359, in tearDown
self.ad_set.remote_delete()
File "facebookads/objects.py", line 709, in remote_delete
self.clear_id()
File "facebookads/objects.py", line 448, in clear_id
del self[self.__class__.Field.id]
File "facebookads/objects.py", line 319, in __delitem__
del self._changes[key]
KeyError: 'id'
======================================================================
ERROR: runTest (__main__.AdSetTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 301, in runTest
self.assert_can_delete(self.subject)
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 192, in assert_can_delete
subject.remote_delete()
File "facebookads/objects.py", line 709, in remote_delete
self.clear_id()
File "facebookads/objects.py", line 448, in clear_id
del self[self.__class__.Field.id]
File "facebookads/objects.py", line 319, in __delitem__
del self._changes[key]
KeyError: 'id'
======================================================================
ERROR: runTest (__main__.AdSetTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 311, in tearDown
self.campaign.remote_delete()
File "facebookads/objects.py", line 709, in remote_delete
self.clear_id()
File "facebookads/objects.py", line 448, in clear_id
del self[self.__class__.Field.id]
File "facebookads/objects.py", line 319, in __delitem__
del self._changes[key]
KeyError: 'id'
======================================================================
ERROR: runTest (__main__.MultiProductAdObjectStorySpecTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 435, in runTest
self.assert_can_delete(creative)
File "/home/aron/src/pp/facebook-python-ads-sdk/facebookads/test/integration.py", line 192, in assert_can_delete
subject.remote_delete()
File "facebookads/objects.py", line 709, in remote_delete
self.clear_id()
File "facebookads/objects.py", line 448, in clear_id
del self[self.__class__.Field.id]
File "facebookads/objects.py", line 319, in __delitem__
del self._changes[key]
KeyError: 'id'
from facebookads import FacebookAdsApi
from facebookads.objects import CustomAudience
my_app_id = <APP_ID>
my_app_secret = '<APP_SECRET>'
my_access_token = '<ACCESS_TOKEN>'
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
AD_ACCOUNT_ID = 1431453467646932
audience = CustomAudience(parent_id='{}'.format(AD_ACCOUNT_ID))
audience.update({
CustomAudience.Field.name: 'someName',
CustomAudience.Field.subtype: CustomAudience.Subtype.custom,
})
audience.remote_create()
When I ran this code it throws an error. But when I change the value of parent_id to 'act_{}'.format(AD_ACCOUNT_ID)'
, the code ran without error. Is this by design or a bug?
I would suggest that there is a check for parent_id whether it has prefix 'act_' or not.
Today we encountered a business-critic issue related to our use of this SDK (see #49).
We're thinking of implementing scripts that would test that this SDK works as expected. Also, I was wondering if you have integration tests. I'm willing to contribute to those if needed. Just say the words :)
Hi there,
We encountered encoding issues when trying to send Swedish or French text. We might want to adapt the following code:
if params:
params = _top_level_param_json_encode(params)
keyvals = ['%s=%s' % (key, urllib.parse.quote(value))
for key, value in params.items()]
call['body'] = '&'.join(keyvals)
At first, we thought (as always with encoding) that we should do it ourselves before handing data to the Api, but the issue is that when reading data, the api will return unicode
object instead of basestring
object, hence it makes it difficult to handle every situation. For instance:
data = {...}
data = {key: value.encode("utf-8") for key, value in data.iteritems()}
creative = AdCreative().remote_create(params=data) # this one will work
creative.remote_read(fields=["name", ...]) # let's say the name contains utf-8
adgroup_name = "Adgroup for " + creative["name"] # here we have utf-8
adgroup_data = {"name": "adgroup_name", ...}
adgroup = AdGroup().remote_create(params=adgroup_data) # this call will fail
The exception will be KeyError: u'\xe9'
when calling the urllib.parse.quote
method.
The fix is fairly easy and easily testable, let me know if you're interested in a PR, I can provide it quickly
And update its links.
While creating adsets using batch
Traceback (most recent call last):
File "./manage.py", line 32, in <module>
execute_from_command_line(sys.argv)
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/web/work4us/python/sjp_app/management/commands/push_sjp_ads_campaign_csv.py", line 49, in handle
adsets = self.create_adsets(campaign, adsets_data)
File "/home/web/work4us/python/sjp_app/management/commands/push_sjp_ads_campaign_csv.py", line 74, in create_adsets
batch.execute()
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/facebookads/api.py", line 439, in execute
self._failure_callbacks[index](inner_fb_response)
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/facebookads/objects.py", line 500, in callback_failure
failure(response)
File "/home/web/work4us/python/ads_app/api/utils.py", line 24, in failure_default_batch_callback
raise response.error()
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/facebookads/api.py", line 111, in error
self.body()
File "/home/web/work4us/venv/local/lib/python2.7/site-packages/facebookads/exceptions.py", line 75, in __init__
self._error['error_data']['blame_field_specs']
KeyError: 'blame_field_specs'
the actual error was:
{u'error': {u'is_transient': False, u'error_subcode': 1487478, u'error_user_title': u'Invalid Geo Locations', u'error_data': {u'blame_field': u'targeting'}, u'error_user_msg': u'Invalid Geo Locations', u'code': 100, u'message': u'Invalid parameter', u'type': u'FacebookApiException'}}
Creating a WCA is failing because pixel_id is not listed as a field.
Adding the text:
pixel_id = 'pixel_id'
Under:
class CustomAudience(AbstractCrudObject):
class Field(object):
fixes this problem
Edit:
Pull Request here: #43
In examples/custom_audience_utils.py line 103
r = audience.add_users(schema, data)
The function add_users()
requires an app_id
parameter as implemented in facebookads/objects.py
.
I'm trying to create several creatives using [page post inline|]. Here is how I create the creative:
def create_creative(batch, ...):
params[AdCreative.Field.object_story_spec] = {
"page_id": page_id,
"link_data": {
"message": "<name>",
"name": "<name>",
"picture": "<picture-url>",
"description": "<description>",
"link": "<post-link>"
}
}
creative = AdCreative()
creative.remote_create(params=params, batch=batch)
return creative
And then I do the following:
batch = api.new_batch()
creative1 = create_creative(batch, **data)
creative2 = create_creative(batch, **data)
creative3 = create_creative(batch, **data)
creative4 = create_creative(batch, **data)
creative5 = create_creative(batch, **data)
batch.execute()
The result is the following:
facebookads.exceptions.FacebookRequestError(u'Call was not successful \nRequest:\n\t{\'body\': \'<the body>', \'method\': \'POST\', \'relative_url\': \'act_104145169778295/adcreatives\'}\nResponse:\n\tHTTP Status: 500\n\tHeaders:[{u\'name\': u\'Access-Control-Allow-Origin\', u\'value\': u\'*\'}, {u\'name\': u\'Vary\', u\'value\': u\'Accept-Encoding\'}, {u\'name\': u\'Pragma\', u\'value\': u\'no-cache\'}, {u\'name\': u\'Cache-Control\', u\'value\': u\'no-store\'}, {u\'name\': u\'Content-Type\', u\'value\': u\'text/javascript; charset=UTF-8\'}, {u\'name\': u\'Facebook-API-Version\', u\'value\': u\'v2.3\'}, {u\'name\': u\'WWW-Authenticate\', u\'value\': u\'OAuth "Facebook Platform" "invalid_request" "Service temporarily unavailable"\'}, {u\'name\': u\'Expires\', u\'value\': u\'Sat, 01 Jan 2000 00:00:00 GMT\'}]\n\tBody: {"error":{"message":"Service temporarily unavailable","type":"FacebookApiException","is_transient":false,"code":2,"error_subcode":1487172,"error_user_title":"Could not save creative","error_user_msg":"Could not save creative"}}\n')
It's very strange. I'm sure it's not a pb of the SDK itself, but are you aware of any limitation for creative creation?
Thanks
When calling AdImage().remote_create(batch=batch); batch.execute()
the exception ValueError: I/O operation on closed file
is raised.
When using the AdImage().remote_create()
I found that this worked as intended and the AdImage object was returned with an 'id' and 'hash', but when attempting to do this while using a batch I ran into the I/O exception.
When looking into a possible fix for this, I tried looking at the AdImage's remote_create function (line number 1366 for me) and noticed that open_file
was being closed before the batch could be executed. Commenting out the line that closed the file for the sake of testing made the call complete but an error response {u'error': {u'message': u'File <IMG_PATH> has not been attached', u'type': u'GraphBatchException'}}
was returned.
Here is the script I started with:
from facebookads import FacebookSession, FacebookAdsApi
from facebookads.objects import AdAccount, AdImage
APP_ID= "<APP_ID>"
APP_SECRET = "<APP_SECRET>"
ACCESS_TOKEN="<ACCESS_TOKEN"
IMG_PATH= "<IMG PATH>"
session = FacebookSession(APP_ID, APP_SECRET, ACCESS_TOKEN)
api = FacebookAdsApi(session)
FacebookAdsApi.set_default_api(api)
account = AdAccount.get_my_account()
img = AdImage(parent_id=account.get_id_assured())
img[AdImage.Field.filename] = IMG_PATH
batch = account.get_api_assured().new_batch()
img.remote_create(batch=batch)
batch.execute()
Here is the stack trace that was outputted:
File "/Users/nilesnelson/projects/facebook-python-ads-sdk/facebookads/api.py", line 416, in execute
files=files,
File "/Users/nilesnelson/projects/facebook-python-ads-sdk/facebookads/api.py", line 270, in call
files=files,
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/sessions.py", line 443, in request
prep = self.prepare_request(req)
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/sessions.py", line 374, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/models.py", line 307, in prepare
self.prepare_body(data, files, json)
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/models.py", line 449, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/Users/nilesnelson/projects/virtualenvs/adroll/lib/python2.7/site-packages/requests/models.py", line 152, in _encode_files
rf = RequestField(name=k, data=fp.read(),
ValueError: I/O operation on closed file
Here is a test for integration.py that should do the same thing as the script:
class AdImageBatchTestCase(FacebookAdsTestCase):
def setup_ad_image(self):
img = objects.AdImage(
parent_id=self.TEST_ACCOUNT.get_id_assured(),
)
self.delete_in_teardown(img)
img[objects.AdImage.Field.filename] = self.TEST_IMAGE_PATH
return img
def test_batch_create_ad_image(self):
img = self.create_ad_image()
batch = FacebookAdsTestCase.TEST_API.new_batch()
img.remote_create(batch=batch)
batch.execute()
self.assertNotEquals(img['id'], None)
pip install facebookads
Successfully installed facebookads-2.4.0
Traceback (most recent call last):
File "demo_cpa.py", line 6, in
from facebookads import FacebookSession
File "/Library/Python/2.7/site-packages/facebookads/init.py", line 22, in
from facebookads.api import FacebookAdsApi
File "/Library/Python/2.7/site-packages/facebookads/api.py", line 30, in
from facebookads.utils import urls
ImportError: No module named utils
The objective class is missing the VIDEO_VIEWS property.
Nowhere in the API documentation is mentioned the fact that it is possible to add labels on creatives. So I think you code should be updated and remove the HasAdLabel
mixin inheritance from the AdCreative
class here. If I'm mistaken, your API doc should be updated...
Got the following error:
in call raise fb_response.error() FacebookRequestError: Message: Call was not successful Method: GET Path: https://graph.facebook.com/v2.4/6034071669641/adlabels Params: {'summary': 'true'} Status: 400 Response: { "error": { "message": "(#100) Invalid edge (adlabels) on node type (AdCreative)", "code": 100, "type": "OAuthException", "fbtrace_id": "DElUVn10w2E" } }
The sample code not working
from facebookads.objects import AdCampaign
campaign = AdCampaign('<AD_CAMPAIGN_ID>')
params = {
'date_preset': AdCampaign.Preset.last_7_days,
}
insights = campaign.get_insights(params=params)
print insights
Hi guys,
Thanks a lot for making this, but I meet one error when do batch remote_create of campaign/adset/ads and I am not sure whether it's a mistake.
My code like:
api_batch = api.new_batch()
// def callback function
campaign.update({
AdCampaign.Field.name: 'Seattle Ad Campaign',
AdCampaign.Field.objective: AdCampaign.Objective.website_clicks,
AdCampaign.Field.status: AdCampaign.Status.paused,
})
campaign.remote_create(batch=api_batch,success=callback_success,failure=callback_failure)
api.execute()
And it raises error like:
Traceback (most recent call last):
File "create_ad.py", line 164, in <module>
campaign.remote_create(batch=api_batch,success=callback_success,failure=callback_failure)
File "/home/chutong/fb_api_test/facebookads/objects.py", line 529, in remote_create
failure=callback_failure,
File "/home/chutong/fb_api_test/facebookads/api.py", line 373, in add
keyvals.append("%s=%s" % (key, urllib.parse.quote(params[key])))
File "/usr/local/lib/python2.7/urllib.py", line 1225, in quote
raise TypeError('None object cannot be quoted')
TypeError: None object cannot be quoted
The reason is in
for key in params:
keyvals.append("%s=%s" % (key, urllib.parse.quote(params[key])))
params contains key='id' which has value None, it couldn't be treated as string here.
Should a judgement for value added here or do I make a wrong usage of batch remote create here?
Thanks for your help.
Hi,
when trying to get reachestimate with an AdAccount object, build_objects_from_response can't build the object from the response.
The problem seems to be that the response includes 'data' as a dict, and not as a list. (like in the reachestimate response of AdGroups)
AdAccount response:
{
"users": 3300,
"bid_estimations": [
{
"unsupported": false,
"location": 3,
"cpa_min": 110,
"cpa_median": 202,
"cpa_max": 312,
"cpc_min": 31,
"cpc_median": 57,
"cpc_max": 87,
"cpm_min": 32,
"cpm_median": 92,
"cpm_max": 187
}
],
"estimate_ready": true,
"data": {
"users": 3300,
"bid_estimations": [
{
"unsupported": false,
"location": 3,
"cpa_min": 110,
"cpa_median": 202,
"cpa_max": 312,
"cpc_min": 31,
"cpc_median": 57,
"cpc_max": 87,
"cpm_min": 32,
"cpm_median": 92,
"cpm_max": 187
}
],
"estimate_ready": true
}
}
AdGroup response:
{
"data": [
{
"users": 160000,
"bid_estimations": [
{
"unsupported": false,
"location": 3,
"cpa_min": 114,
"cpa_median": 211,
"cpa_max": 300,
"cpc_min": 23,
"cpc_median": 41,
"cpc_max": 60,
"cpm_min": 221,
"cpm_median": 526,
"cpm_max": 925
}
],
"estimate_ready": true
}
]
}
I cannot find problem with my code.
from __future__ import absolute_import, division, print_function
from facebookads.api import FacebookAdsApi
from facebookads import objects
from pprint import pprint
FacebookAdsApi.init(app_id, app_secret,
access_token)
me = objects.AdUser(fbid='me')
for account in me.get_ad_accounts():
for i in account.get_insights(fields=[
objects.Insights.Field.spend,
objects.Insights.Field.campaign_name,
objects.Insights.Field.campaign_id,
], params={'date_preset': 'last_7_days', 'level': 'campaign', 'filtering':
[
{
'field': 'campaign_name',
'operator': 'CONTAIN',
'value': 'hrooom-brooom',
},
{
'field': 'spend',
'operator': 'GREATER_THAN',
'value': 0,
},
]
}):
pprint(i)
i try filter campaigns by part campaign_name (its postfix in real), but returns all campaigns of account. Where is my mistake?
I got an "Undefined class constant 'RETENTION_SECONDS'" error using the following code:
$audience = new ProductAudience(null, 'act_<ACCOUNT_ID>');
$audience->setData(array(
ProductAudienceFields::NAME => 'Product Audience',
ProductAudienceFields::PRODUCT_SET_ID => '<PRODUCT_SET_ID>',
ProductAudienceFields::PIXEL_ID => '<PIXEL_ID>',
ProductAudienceFields::INCLUSIONS => array(array(
ProductAudienceFields::RETENTION_SECONDS => 1296000,
ProductAudienceFields::RULE => array(
'event'=>array('eq'=>'ViewContent')
)
))
));
$audience->save();
I managed to fix it by adding the following constants to the ProductAudienceFields abstract class of ProductAudienceFields.php file in FacebookAds\Object\Fields :
const RETENTION_SECONDS = 'retention_seconds';
const RULE = 'rule';
When adding a remote_create call to a batch an error occurs where the None value for id cannot be properly quoted by urllib.parse.quote
. At this point in the add
call the params should already be valid JSON thanks to _top_level_param_json_encode
. Unfortunately, this function only attempts to apply json.dumps
to a value if it is an instance of collections.Mapping
, collections.Sequence
, or bool
and is not an instance of six.string_types
. None
evades this conditional and is left unmodified in the parameters leaving it to fail when it is passed later as a parameter.
filename
is definitely a string containing path and name. But sometimes it would be useful passing just a file-like object because we have the file only in memory. We could modify the methods to process also file object but the parameter's name is still filename
. So it could be misleading.
Any suggestion?
This bug was validated in the Ads API v2.4 Q&A:
https://www.facebook.com/events/873155332753388/878318465570408/
Reposting this here for better issue tracking
Hello.
I try to get estiamation by this documentation https://www.facebookmarketingdevelopers.com/docs/python but i don't receive cpa_curve_data
field.
What do I wrong?
It doesn't look like this was implemented until v2.4, but it is deprecated in v2.4 on the server-side. It exists in the documentation for v2.3, but it's not in any of the code as far as I can tell (please let me know if it is). It is in the docs for v2.4 too, but the calls are deprecated on the server side and fail! FML.
It looks like a backport would need at least AdAccount.iterage_edge_async and objects.AsyncJob.
Is there a chance we can get this backported to v2.3? I can help. Are there any huge gotchas that make such a backport difficult?
Sucks that it's in the docs, but doesn't work anywhere. Seems like v2.3 is the best shot at providing a workaround. Any guidance is greatly appreciated.
When I create an AdUser
with a specific api instance and then call get_ad_account
on that instance the api instance does not flow to the created AdAccount
.
Here is a test case that reproduces this issue:
app_id = config['app_id']
app_secret = config['app_secret']
access_token = config['access_token']
def test_get_ad_account_from_ad_user():
assert FacebookAdsApi.get_default_api() == None # Ensure the default api is not set
session = FacebookSession(
app_id,
app_secret,
connection.access_token,
)
api = FacebookAdsApi(session)
me = AdUser(fbid='me', api=api)
ad_account = me.get_ad_account()
assert ad_account.get_api() == api
I have one question that really confused me about statistics.
When I try to fetch stats in [start_time, end_time] recently, like below:
account = AdAccount("act_<ACT_ID>")
params = {
'adgroup_ids' : <JSON ID STR>,
'start_time' : <UNIX_TIME>, # 20150319:10:00, here is 1426730400
'end_time' : <UNIX_TIME>, # 20150319:11:00, here is 1426734000
}
adgroup_stats_list = account.get_ad_group_stats(params=params)
for adgroup_stats in adgroup_list:
print adgroup_stats['adgroup_id']
print adgroup_stats['clicks']
print adgroup_stats['impressions']
print adgroup_stats['actions']['mobile_app_install']
it returns stats not in [10:00, 11:00] , but [0:00, current_time], it seems start_time and end_time not useful in hour level, however, when I used the API one month ago, it returns clicks and impressions in [10:00, 11:00], it seems sth changed, and I'm not sure the standard behavior for this API call.
Could anyone answer my question? thanks a lot
In the Insights documentation, the stated return type for the Impressions general field is said to be of type Int:
When I am making a get_insight call on an AdAccount object, however, the Impressions field is returned as a unicode string. Here's an example call:
account = AdAccount(account_id)
params = {
'fields': [Insights.Field.impressions,],
'time_range': {
'since': '2015-05-15',
'until': '2015-05-15',
},
}
stats = account.get_insights(params = params)
print stats
What is returned looks like this, and calling type() on the value for "impressions" returns unicode:
[<Insights> {
"date_start": "2015-05-15",
"date_stop": "2015-05-15",
"impressions": "27155"
}]
Am I missing something here, or are impressions just incorrectly being returned as a unicode string?
I just changed the version in api.py to:
API_VERSION = 'v2.4'
I think is necessary to add facebook.utils in setup.py to PACKAGES
PACKAGES = ['facebookads', 'facebookads.test', 'facebookads.utils']
Currently you are unable to upload pre-hashed SHA-256 hashes since the add_user function does the hashing automatically. There should be an option or logic implemented to upload emails that are already hashed
When I get my ad accounts, it throw a exceptions "AttributeError: 'unicode' object has no attribute 'get'".
File "/app/services/ads/facebook.py", line 1016, in
FacebookAPI = FacebookAds(my_app_id, my_app_secret, my_access_token)
File "/app/services/ads/facebook.py", line 51, in init
self.my_accounts = list(me.get_ad_accounts())
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/objects.py", line 797, in get_ad_accounts
return self.iterate_edge(AdAccount, fields, params)
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/objects.py", line 745, in iterate_edge
params=params
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/objects.py", line 94, in init
self.load_next_page()
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/objects.py", line 137, in load_next_page
params=self._params,
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/api.py", line 307, in call
raise fb_response.error()
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/api.py", line 117, in error
self.body()
File "/app/.heroku/python/lib/python2.7/site-packages/facebookads/exceptions.py", line 76, in init
if self._error.get('error_data', {}).get('blame_field_specs'):
AttributeError: 'unicode' object has no attribute 'get'
Hi there,
I'm encountering some issues with the following code:
def __next__(self):
# Load next page at end.
# If load_next_page returns False, raise StopIteration exception
if not self._queue and not self.load_next_page():
raise StopIteration()
return self._queue.pop(0)
Notice the self.load_next_page()
. This is cool for scripting, because people have time to wait until the N HTTP requests are over. But I'm using this in production environment in one of our APIs. And infinite loading does not play well with cloudflare timeout. I know this is particular to my setup but I really feel like I might not be the only one encountering that problem. I manage to circumvent this by overriding the iterate_edge
methods of my objects and passing a custom EdgeIterator
that does not load the next page systematically. Instead, I call manually load_next_page()
when needed.
I understand that you cannot change your code API only for one user, but would you consider adding a parameter (for instance infinite_loading
) True
by default (or False
if you really like the spirit!) that would control this behaviour?
If you're interested, I can provide a PR quickly, with a proof of concept :)
Cheers
When trying to upgrade (or doing a fresh install) of facebookads 0.20 you get this error:
$ pip install --upgrade facebookads==0.2.0
Downloading/unpacking facebookads
Downloading facebookads-0.2.0.tar.gz (377kB): 377kB downloaded
Running setup.py (path:/home/vagrant/env/build/facebookads/setup.py) egg_info for package facebookads
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/home/vagrant/env/build/facebookads/setup.py", line 46, in <module>
with open(readme_filename) as f:
IOError: [Errno 2] No such file or directory: '/home/vagrant/env/build/facebookads/README.md'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/home/vagrant/env/build/facebookads/setup.py", line 46, in <module>
with open(readme_filename) as f:
IOError: [Errno 2] No such file or directory: '/home/vagrant/env/build/facebookads/README.md'
Should genereate account like this:
my_account = AdAccount('act_' + config['app_act_id'])
Otherwise, it is not working.
Because, according to the link, https://developers.facebook.com/docs/marketing-api/custom-audience-targeting/v2.3#examples
curl request with parameter: act_<AD_ACCOUNT_ID>
example
read existing product sets with field names
http://graph.facebook.com/{product_catalogue_id}/product_sets?fields=product_count,name
I can't create a LookalikeAudience:
lookalike = LookalikeAudience(parent_id=self.ad_account_id)
It has a error "type object 'Field' has no attribute 'id". In your API, class LookalikeAudience don't have id in the Field class. Is this a bug?
(Sorry if I'm not clear as I can be, I'm recovering from a surgery atm)
If I want to return any fields more than just id
, I have to enter fields
by hand, this is a bit cumbersome:
# note I already have the API session object inside (as in the README.md example)
def fetch_ad_sets(fbid):
# I have :class:`facebookads.objects.AdCampaign` here
campaign = fb_objects.AdCampaign(fbid=fbid)
campaign = campaign.remote_read(
params={"id": fbid},
# Get all the keys inside of a class, useful.
# fields=[attr for attr in dir(fb_objects.AdCampaign.Field) if not callable(getattr(fb_objects.AdCampaign.Field, attr)) and not attr.startswith("__")]
fields=[
fb_objects.AdCampaign.Field.buying_type,
fb_objects.AdCampaign.Field.name,
fb_objects.AdCampaign.Field.objective,
fb_objects.AdCampaign.Field.status,
]
)
ad_sets = campaign.get_ad_sets(
# only returns ID's
# fields=facebookads.objects.AdSet.get_default_read_fields()
# returns everything I specify, but it's manual
fields=[
fb_objects.AdSet.Field.bid_info,
fb_objects.AdSet.Field.bid_type,
fb_objects.AdSet.Field.budget_remaining,
fb_objects.AdSet.Field.campaign_group_id,
fb_objects.AdSet.Field.created_time,
fb_objects.AdSet.Field.daily_budget,
fb_objects.AdSet.Field.end_time,
fb_objects.AdSet.Field.lifetime_budget,
fb_objects.AdSet.Field.name,
fb_objects.AdSet.Field.start_time,
fb_objects.AdSet.Field.status,
fb_objects.AdSet.Field.targeting,
fb_objects.AdSet.Field.updated_time,
]
)
for ad_set in ad_sets:
print('FbCampaign %s Ad Set: %s' % (campaign, ad_set))
# this would retrieve everything,
# but everything else, including no ``field`` param returns just ``'id'`` for me.
for ad_group in ad_set.get_ad_groups(fields=facebookads.objects.AdGroup.get_default_read_fields()):
print('FbCampaign %s Ad Set %s Ad Group: %s' % (campaign, ad_set, ad_group))
I have tried various things to try to get the fields
param to work when doing remote_read
and padding directly into a object class, however, the only way fields work now is if I do it explicitly.
Here are other things I've tried:
fields=[attr for attr in dir(fb_objects.AdCampaign.Field) if not callable(getattr(fb_objects.AdCampaign.Field, attr)) and not attr.startswith("__")]
fields=facebookads.objects.AdSet.get_default_read_fields()
# not entering fields param at all
All return just the 'id'
.
see those lines https://github.com/facebook/facebook-python-ads-sdk/blob/master/facebookads/objects.py#L146-L147. We expected it to be set every time, and we used the total()
method that returns None
, which is really bad because it silently tells that there is no stats. Instead:
Exception
so that we do not fail silently?I can come up with a PR if you want
Hello,
I am currently using version 2.4
I found that I am not able to set bid_type for the ad set that I create. There is no bid_type member available in the AdSet object. I would like to set the bid_type to CPC. The ad set that I create defaults to CPA.
How to work this issue ? Any idea how to set bid_type of the ad set to CPC
Thanks
Sibi
There is currently no straightforward way to fetch Ad Report Stats through asynchronous queries. get_report_stats
in AdAccount
only allows direct GET
requests. And most historical data is not accessible through it, returning error 1487535. Will this be implemented in the future?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.