Get analytics data errors

v1
analytics

#1

Hi there! I’m facing a problem with getting analytics data for a promoted tweet using the endpoint GET /1/stats/accounts/:account_id.

I’m not sure if this endpoint should have the .json suffix or not, as other endpoints must have (for example, the endpoint for campain editing works only with the “.json” suffix, even though this doesn’t appear on the endpoint’s documentation page).

I have made 2 requests, with the suffix and without the suffix, and both return an error, not the same one though:

GET /1/stats/accounts/18ce53z82f8.json?entity=PROMOTED_TWEET&entity_ids=cn1iw&granularity=DAY&start_time=2015-09-07T07%3A00%3A00Z&end_time=2015-09-11T07%3A00%3A00Z&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER

returns

HTTP/1.1 400 Bad Request
{"errors":[{"code":"MISSING_PARAMETER","message":"\"account_id\" is a required parameter","parameter":"account_id"}],"request":{"params":{}}}

and

GET /1/stats/accounts/18ce53z82f8?entity=PROMOTED_TWEET&entity_ids=cn1iw&granularity=DAY&start_time=2015-09-07T07%3A00%3A00Z&end_time=2015-09-11T07%3A00%3A00Z&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER

returns

HTTP/1.1 401 Authorization Required
{"errors":[{"code":"UNAUTHORIZED_ACCESS","message":"This request is not properly authenticated"}],"request":{"params":{}}}

As far as I can see, the parameters are not recognized either.

The account is in the America/Denver timezone, which is UTC-7.

Could anyone help me with this, please? I can’t figure out what I’m doing wrong.

Thank you!


#2

Anyone?..


#3

It looks like you’re maybe formatting bad the request (the date)

Take a look my request:

twurl -H https://ads-api.twitter.com '/1/stats/accounts/18ce53yv4fv?entity=PROMOTED_TWEET&entity_ids=cn1iw&granularity=DAY&start_time=2015-09-07T22:00:00Z&end_time=2015-09-11T22:00:00Z&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER'

My account is at UTC+2


#4

Thank you, @hector_borras.
I believe it’s normal for the parameters to be url-encoded. Either way, like this, I receive the error

HTTP/1.1 401 Authorization Required
{"errors":[{"code":"UNAUTHORIZED_ACCESS","message":"This request is not properly authenticated"}],"request":{"params":{}}}

…with or without the “.json” suffix:

GET /1/stats/accounts/18ce53z82f8?end_time=2015-09-11T06:00:00Z&entity=PROMOTED_TWEET&entity_ids=cn1iw&granularity=DAY&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER&start_time=2015-09-07T06:00:00Z HTTP/1.1
Host: ads-api.twitter.com

GET /1/stats/accounts/18ce53z82f8.json?end_time=2015-09-11T06:00:00Z&entity=PROMOTED_TWEET&entity_ids=cn1iw&granularity=DAY&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER&start_time=2015-09-07T06:00:00Z HTTP/1.1
Host: ads-api.twitter.com

#5

@majoritasdev,

I don’t know how are you making this request.

Try it with twurl insead of your own library/code. Seems like is a problem witth the authorization.

Copy and paste the twurl request and response :smiley:

Regards!


#6

Finally, I found out what the problem was… The v1 analytics endpoints do not work with the .json extension. The other endpoints do, even though it doesn’t appear in each endpoint’s documentation. So I didn’t find this to be documented - apologies if I’m mistaken. I was removing the .json extension just before the curl call was made, just for testing purposes, but I gather it was being used somewhere else too, so adding the extension only for non-analytics endpoints at all, at the point where it was firstly added solved the problem. :slight_smile:


#7

Good @majoritasdev,

I don’t use .json in any endpoint in the Twitter ADS API :smiley:


#8

I see, thanks for that info! Then, I guess only the REST APIs require the .json extension. (I am using a library that I extended in order to work with the Ads API, which was built for the REST API and the extension remained there, and I haven’t had any problems with it so far; that’s why I was/am using this extension.)


#9

Are you doing it in PHP? Is the library from Abraham? I’ve forked it and edited in order to extend it and make it compatible with the ads api


#10

Yes, and yes. I’ve edited it too, locally, to work with the Ads API. But thank you, @hector_borras .


#11

Hi @majoritasdev, I am getting the issue same like you, when trying this API twurl -t -H https://ads-api.twitter.com “/1/stats/accounts/18ce54h6gph?start_time=2017-03-21T00:00:00Z&end_time=2017-03-23T00:00:00Z&granularity=DAY&entity=CAMPAIGN&entity_ids=7utab&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER” with twurl then it works fine. But when trying to implement the same in PHP then getting error below

    [errors] => Array
    (
        [0] => stdClass Object
            (
                [code] => UNAUTHORIZED_ACCESS
                [message] => This request is not properly authenticated
            )

    )

PHP CODE

$consumer_key = 'xxxxxxx';
$consumer_secret = 'xxxxx';
$oauth_access_token = 'xxxxxxx';
$oauth_access_token_secret = 'xxxxxxx';
$twitter_rss_base = "https://ads-api.twitter.com/1/stats/accounts/18ce54h6gph";
$twitter_rss = $twitter_rss_base."?start_time=2017-03-21T00:00:00Z&end_time=2017-03-23T00:00:00Z&granularity=DAY&entity=CAMPAIGN&entity_ids=7utab&metric_groups=ENGAGEMENT&    placement=ALL_ON_TWITTER";
$oauth = array(
'end_time' => '2017-03-23T00:00:00Z',
'entity' => 'CAMPAIGN',
'entity_ids' => '7utab',
'granularity' => 'DAY',
'metric_groups' => 'ENGAGEMENT',
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $oauth_access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
'placement' => 'ALL_ON_TWITTER',
'start_time' => '2017-03-21T00:00:00Z'
);

$base_info = buildBaseString($twitter_rss_base, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' .rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;

$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $twitter_rss,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false);

$ch = curl_init();
curl_setopt_array($ch, $options);
$twitterJson = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
echo '<pre>';

$json = json_decode($twitterJson);
print_r($json);

same type of code also working for other APIs. But working for above API
Thanks.


#12

Sometimes URL errors can manifest in the same error code, was this empty space a copy paste issue or is this something to look at? “metric_groups=ENGAGEMENT& placement=ALL_ON_TWITTER”


#13

@JBabichJapan, Thank you for you reply on this. I have fixed the issue. There was not copy past issue. issue was related to start_time and end_time.