问题
I run a query on Facebook Graph API Explorer successfully but when I run the same query on Google Apps Script with UrlFetchApp I get an Error - Invalid argument. The query is this:
https: //graph.facebook.com/v4.0/act_1015703131******/insights?fields=spend&level=account&date_preset=yesterday&filtering=[{field:campaign.name,operator:CONTAIN,value:perf},{field:adset.name,operator:NOT_CONTAIN,value:rmk}]&access_token=********
When I run the same query without filtering it works as expected!
Json Result:
EDIT: I tried to encode the filtering parameters and it throws a new error:
Request failed for https://graph.facebook.com returned code 400. Truncated server response: {"error":{"message":"(#100) param filtering must be an array.","type":"OAuthException","code":100,"fbtrace_id":"AMw2vkaHXbuiVSQuSNg28yZ"}} (use muteHttpExceptions option to examine full response)
What is causing the problem exactly since the error doesn't give any specific description?
回答1:
First put your filtering value into an array:
var fbCallFiltering = [{'field':'campaign.name','operator':'CONTAIN','value':'perf'},{'field':'adset.name','operator':'NOT_CONTAIN','value':'rmk'}];
Then use JSON.stringify()
var fbCallFilteringString = encodeURIComponent(JSON.stringify(fbCallFiltering));
Finally use the fbCallFilteringString
variable in the URL you use to make the UrlFetchApp call after the &filtering=
.
This should work.
来源:https://stackoverflow.com/questions/58658777/facebook-graph-api-not-working-on-urlfetchapp