POST Method Not Working - Ads API

php

#1

Hi,

I have created a custom PHP library(using PHP cURL) and it working fine for Ads API with GET method. When trying with POST method then getting below response.

Response : {“errors”:[{“code”:“UNAUTHORIZED_ACCESS”,“message”:“This request is not properly authenticated”}],“request”:{“params”:{}}}

Header Information for request

POST /1/stats/jobs/accounts/{account_id} HTTP/1.1
Host: ads-api.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="{oauth_consumer_key}",oauth_nonce="1494845304", oauth_signature="CCkH3%2F6VhEcuHVobCVpjRqTu6T4%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1494845304", oauth_token="{oauth_token}", oauth_version="1.0"
Content-Length: 179
Content-Type: application/x-www-form-urlencoded

Could anyone help me to figure out the issue?

OR
Do you have another endpoint instead of below endpoint, Please share
POST /1/stats/jobs/accounts/:account_id

============================================================
I have also tried like below

1.
POST /1/stats/jobs/accounts/{account_id} HTTP/1.1
Host: ads-api.twitter.com
Accept: */*
Authorization: OAuth end_time="2017-04-25", entity="CAMPAIGN", entity_ids="xxxxx", granularity="TOTAL", metric_groups="ENGAGEMENT", oauth_consumer_key="{oauth_consumer_key}", oauth_nonce="1494848118", oauth_signature="bPczRqTptu9%2FzzYm3SrjM7nG4b8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1494848118", oauth_token="{oauth_token}", oauth_version="1.0", placement="ALL_ON_TWITTER", platform="0", segmentation_type="DEVICES", start_time="2017-04-24"
Content-Length: 179
Content-Type: application/x-www-form-urlencoded

2.
POST /1/stats/jobs/accounts/{account_id}?end_time=2017-04-25&entity=CAMPAIGN&entity_ids=86onf&granularity=TOTAL&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER&start_time=2017-04-24&segmentation_type=DEVICES&platform=0 HTTP/1.1
Host: ads-api.twitter.com
Accept: */*
Authorization: OAuth end_time="2017-04-25", entity="CAMPAIGN", entity_ids="xxxxx", granularity="TOTAL", metric_groups="ENGAGEMENT", oauth_consumer_key="{oauth_consumer_key}", oauth_nonce="1494848461", oauth_signature="OHTBboE%2BSnTbDbTJQdfDbrXQCI4%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1494848461", oauth_token="{oauth_token}", oauth_version="1.0", placement="ALL_ON_TWITTER", platform="0", segmentation_type="DEVICES", start_time="2017-04-24"
Content-Length: 179
Content-Type: application/x-www-form-urlencoded

#2

You should send your cURL request as follows:

// Your cURL options...
$options = [---];

$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = buildHttpQuery($postfields);

$curlHandle = curl_init();
curl_setopt_array($curlHandle, $options);
$response = curl_exec($curlHandle);


public static function buildHttpQuery($params)
    {
        if (!$params) {
            return '';
        }

        // Urlencode both keys and values
        $keys = Util::urlencodeRfc3986(array_keys($params));
        $values = Util::urlencodeRfc3986(array_values($params));
        $params = array_combine($keys, $values);

        // Parameters are sorted by name, using lexicographical byte value ordering.
        // Ref: Spec: 9.1.1 (1)
        uksort($params, 'strcmp');

        $pairs = [];
        foreach ($params as $parameter => $value) {
            if (is_array($value)) {
                // If two or more parameters share the same name, they are sorted by their value
                // Ref: Spec: 9.1.1 (1)
                // June 12th, 2010 - changed to sort because of issue 164 by hidetaka
                sort($value, SORT_STRING);
                foreach ($value as $duplicateValue) {
                    $pairs[] = $parameter . '=' . $duplicateValue;
                }
            } else {
                $pairs[] = $parameter . '=' . $value;
            }
        }
        // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
        // Each name-value pair is separated by an '&' character (ASCII code 38)
        return implode('&', $pairs);
    }

#3

Hi @hector_borras, I am sending header three types above, Which one would be perfect with you function?


#4

Hi @hector_borras, Still facing same issue after using your function.

Your function returning the string : "end_time=2017-04-25&entity=CAMPAIGN&entity_ids=xxxxx&granularity=TOTAL&metric_groups=ENGAGEMENT&placement=ALL_ON_TWITTER&platform=0&segmentation_type=DEVICES&start_time=2017-04-24"
Is it fine?


#5

Hi @deansharedemo!

Can you please try your request using Twurl with the following example POST request ->
https://dev.twitter.com/ads/reference/1/post/stats/jobs/accounts/account_id

Make sure to use -t in the Twurl request to spit out trace-route in order for us to help you more. Thanks!


#6

Hi @goforbrent and @hector_borras,

FYI, I have resolved the issue itself.

Thank you so much for your support.


#7

Hi @deansharedemo,

Could you explain how did you solve it?

Thanks


#8

Hi @hector_borras, There was issue in authorization for POST method. So, I have changed the authorization related code for POST method.