linkedin-apilinkedin-jsapi

Getting 400 Bad request while acquiring AudienceCountsV2 in Linkedin API


I am using the LinkedIn API V2 and I am trying to query the AudienceCountsV2 from my company profile. I am using the following tutorial to build the request:

https://developer.linkedin.com/docs/guide/v2/ads/audience-counts#targetingCriteriaV2  

This is the request information:

GET /v2/audienceCountsV2?q=targetingCriteriaV2&targetingCriteria=(include:(and:List((or:(urn%3Ali%3AadTargetingFacet%3Alocations:List(urn%3Ali%3AcountryGroup%3Ana))),(or:(urn%3Ali%3AadTargetingFacet%3Askills:List(urn%3Ali%3Askill%3A17)))))) HTTP/1.1 
Host: api.linkedin.com 
X-Restli-Protocol-Version: 2.0.0 
Authorization: Bearer XXXXXXX 
Cache-Control: no-cache

This is the response information:

HTTP/1.1 400 Bad Request   
x-restli-gateway-error:"true"
content-type:"application/json" 
date:"Wed, 12 Sep 2018 09:46:05 GMT" 
x-li-fabric:"prod-ltx1" 
transfer-encoding:"chunked" 
connection:"keep-alive" 
x-li-pop:"PROD-IDB2" 
x-li-proto:"http/1.1" 
set-cookie:"lidc="b=TB07:g=1965:u=1401:i=1536745565:t=1536824643:s=AQHBwKwS4Rsf4yI0kKGnz8pQwqOmeVu0"" 
x-li-route-key:""b=TB07:g=1965:u=1401:i=1536745565:t=1536824643:s=AQHBwKwS4Rsf4yI0kKGnz8pQwqOmeVu0"" 
x-li-uuid:"EWbxLvOdUxXAHJ7aEysAAA=="  
serviceErrorCode:0 
message:"Invalid query parameters passed to request"

When I am trying the same request with TargetingCriteriaV1 I do not have the issue and everything is working fine.


Solution

  • The problem in this case was that the encoding was not done properly on my side. I was encoding all the ":" symbols while the API has a restriction to encode only a specific part of target criteria object. For example:

    targetingCriteria=(include:(and:List((or:(urn:li:adTargetingFacet:locations:List(urn:li:countryGroup:na))),(or:(urn:li:adTargetingFacet:skills:List(urn:li:skill:17))))))
    

    was encoded to

    targetingCriteria=(include:(and:List((or:(urn%3Ali%3AadTargetingFacet%3Alocations:List(urn%3Ali%3AcountryGroup%3Ana))),(or:(urn%3Ali%3AadTargetingFacet%3Askills:List(urn%3Ali%3Askill%3A17))))))