How to get Ad campaign performance data through api


#1

Here is what I’m trying to get through twitter ads api
Time period
Placement
Campaign name
Objective
Edit url
Status
Impressions
Spend
Purchases
Cost per purchase
Purchases - sale amount
Link clicks
Cost per link click
Campaign start
Campaign end
Total budget
Results
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


#2

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


#3

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
https://ads-api.twitter.com/4/accounts/18ce53zx1fm/campaigns
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 “https://ads-api.twitter.com/4/accounts/18ce53zx1fm/campaigns” which worked fine.

Then I tried to get the Ad performance report and below is the endpoint
https://ads-api.twitter.com/4/stats/accounts/accountId?granularity=DAY&placement=ALL_ON_TWITTER&start_time=2018-11-26&end_time=2018-11-28&entity=CAMPAIGN&entity_ids=(list 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 “https://ads-api.twitter.com/4/stats/accounts/18ce53zx1fm

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


#4

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.


#5

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 = ‘https://ads-api.twitter.com/4/stats/accounts/18ce53zx1fm
$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)));
[System.Uri]::EscapeDataString($oauth_signature)

$Endpoint = ‘https://ads-api.twitter.com/4/stats/accounts/18ce53zx1fm?granularity=DAY&placement=ALL_ON_TWITTER&start_time=2018-11-26&end_time=2018-11-28&entity=CAMPAIGN&entity_ids=testxyz,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 “https://ads-api.twitter.com/4/accounts/18ce53zx1fm/campaigns” 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.


#6

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.


#7

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.

on https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature#id2
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): https://tools.ietf.org/html/rfc5849#section-3.4.1.3.2

Now you know why it worked with “https://ads-api.twitter.com/4/stats/accounts/18ce53zx1fm” because it just doens’t have any request parameters.

Please use OAuth category for further questions if you have.

Best,
Shohei


Getting UNAUTHORIZED_ACCESS with Ads API
#9

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


#10

@madtrix_io

I’m not sure what’s your point. See below for generating a signature:
https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature


#11

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


closed #12