How to get Ad campaign performance data through api


Here is what I’m trying to get through twitter ads api
Time period
Campaign name
Edit url
Cost per purchase
Purchases - sale amount
Link clicks
Cost per link click
Campaign start
Campaign end
Total budget
Result Type
Result Rate
Result Rate Type
Cost Per Result
Cost Per Result Type
Daily budget
Remaining campaign budget
Campaign daily pacing
Campaign flight pacing
Purchase_Box - Purchases
Purchase_Box - Purchases - post engagement
Purchase_Box - Purchases - post view
Purchase_Box - Purchases - sale amount
Site Visit - Event Tag - Site visits
Site Visit - Event Tag - Site visits - post engagement
Site Visit - Event Tag - Site visits - post view
Site Visit - Event Tag - Site visits - sale amount
Purchase - Single Event - Purchases
Purchase - Single Event - Purchases - post engagement
Purchase - Single Event - Purchases - post view
Purchase - Single Event - Purchases - sale amount
Opt-in - Single-event - Sign ups
Opt-in - Single-event - Sign ups - post engagement
Opt-in - Single-event - Sign ups - post view
Opt-in - Single-event - Sign ups - sale amount


What have you tried doing thus far to get this information?


First I sent the http request to get all the list of campaigns to the below endpoint and request went through successfully both in powershell script and as well as in Postman
that means my logic to create the oauth signature, timestamp, nonce and other credentials provided in the script worked for getting the campaigns list
For the campaign list I used Base URL as “” which worked fine.

Then I tried to get the Ad performance report and below is the endpoint of campaignIds separated by “,”)&metric_groups=ENGAGEMENT,WEB_CONVERSION

But the problem is that I’m getting UNAUTHORIZED error when I tried to do it through powershell script
whereas request is successfull when I used Postman
{“errors”:[{“code”:“UNAUTHORIZED_ACCESS”,“message”:“This request is not properly authenticated”}],“request”:{“params”:{}}}
Base URL for this one is “

is that the correct base URL for the ad performance report?


Yes, that’s the correct URL for our synchronous analytics endpoint.

Please see the following post for information about why you might be seeing the 401 HTTP status code even though the GET accounts/:account_id/campaigns request works.


I’m still having the issue with the synchronous analytics endpoint, the actual solution for my issue is not in that post
I think the problem is with the oauth_signature
Here is my powershell code which is used to create oauth_signature(the oauth_signature generated by this code works fine with the other endpoints but not with analytics endpoint)

$baseurl = ‘
$signature = “GET&”;
$signature += [System.Uri]::EscapeDataString($baseurl) + “&”;
$signature += [System.Uri]::EscapeDataString(“oauth_consumer_key=” + $oauth_consumer_key + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_nonce=” + $oauth_nonce + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_signature_method=HMAC-SHA1&”);
$signature += [System.Uri]::EscapeDataString(“oauth_timestamp=” + $oauth_timestamp + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_token=” + $oauth_token + “&”);
$signature += [System.Uri]::EscapeDataString(“oauth_version=1.0”);

$signature_key = [System.Uri]::EscapeDataString($oauth_consumer_secret) + “&” + [System.Uri]::EscapeDataString($oauth_token_secret);

$hmacsha1 = new-object System.Security.Cryptography.HMACSHA1;
$hmacsha1.Key = [System.Text.Encoding]::ASCII.GetBytes($signature_key);
$oauth_signature = [System.Convert]::ToBase64String($hmacsha1.ComputeHash([System.Text.Encoding]::ASCII.GetBytes($signature)));

$Endpoint = ‘,testabc,&metric_groups=ENGAGEMENT,WEB_CONVERSION

$response = Invoke-WebRequest -Uri $Endpoint -Headers @{‘AUTHORIZATION’=$oauth_authorization} -Method GET

I was able to make the request to the synchronous analytics endpoint successfully through POSTMAN but not through the script
The $oauth_signature generated by the above code, won’t match with the oauth signature generated by the POSTMAN for the same request and same credentails (i.e , timestamp, nonce,oauth_token,consumerkey etc everthing same)

Now the strange thing is that if I change the $baseurl and $Endpoint to “” then it works fine($oauth_signature also generated by the script exactly matches with that of the oauth signature generated by POSTMAN)

Which is why I intially thought that there might be something wrong with the BASE URL but as you confirmed that BASE URL is correct, I’ve no idea why my oauth_signature generated by script is wrong and causing UNAUTHORIZED_ACCESS error

Please help me in finding out the root cause of this issue.


As my previous post states:

Ensure that you’re encoding reserved characters appropriately within URLs and POST bodies before preparing OAuth signature base strings.

For details on this, please see this post, which is linked to from the post I pasted above.

Also, please ensure you are using an OAuth library rather than implementing your own.


Hey @meghana ,

To follow-up @juanshishido’s comment, here’s why you don’t want to handle OAuth authentication manually.

  1. You’re not adding request parameters to “$signature” variable.

  2. And then you must “normalize” request parameters.

See “Collecting parameters” section very carefully`…

  1. Percent encode every key and value that will be signed.
  2. Sort the list of parameters alphabetically [1] by encoded key [2].

[1] Note: The OAuth spec says to sort lexigraphically, which is the default alphabetical sort for many libraries.

RFC spec (OAuth 1.0):

Now you know why it worked with “” because it just doens’t have any request parameters.

Please use OAuth category for further questions if you have.



Hi, How did you generate the oauth_signature? or what elements did you use it when you generated it?



I’m not sure what’s your point. See below for generating a signature:


hi, I had trouble on generating the signature and making a request, but now I have figure it out! :slight_smile:

closed #12