skip to Main Content

I am trying to perform a request to the twitter Ads API in my dev environment. I am already registered to get access to this service.

I have received a confirmation e-mail like this:

Your application (ID:12345678) has been approved for the Twitter Ads API program and your organization has been granted a Developer license for Read/Write access. ...

This is why I suppose to have my APP ready to query the Ads API.

Besides that I have information about the APP (tokens and secrets) in the page https://developer.twitter.com/en/apps but I can’t find any reference to the account_id, mentioned in the official documentation.

Advertising accounts are registered on ads.twitter.com and identified
in the API by account_id. Advertising accounts link directly to funding
sources and leverage content from one or more Twitter user accounts as 
‘promotable users’. Each advertising account can grant permission to 
one or more Twitter user accounts. The advertising account, or “current 
account,” is represented in nearly every URL executed as an in-line 
:account_id parameter.

Following this post I have create the follow code in oder to get access to the Twitter Ads API:

$settings = array(
    'oauth_access_token'        => env('TWITTER_ACCESS_TOKEN'),
    'oauth_access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
    'consumer_key'              => env('TWITTER_CONSUMER_KEY'),
    'consumer_secret'           => env('TWITTER_CONSUMER_SECRET'),
);

$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=J7mbo';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchangeService($settings);
$data = $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();
dd($data);

The previous code is working (I am not querying Ads API. But the next one ( querying the Ads Api) is not working:

$url = 'https://ads-api.twitter.com/5/accounts';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchangeService($settings);
$data = $twitter->buildOauth($url, $requestMethod)->performRequest();

dd($data);

{"errors":[{"code":"INSUFFICIENT_USER_AUTHORIZED_PERMISSION","message":"User 2222222222 is not authorized to make this request. Please have them reauthorize with your client application APPNAme."}],"request":{"params":{}}}

What am I missing?

2

Answers


  1. Chosen as BEST ANSWER

    I have found a solution. I don't know if this is the only one but it works.

    We must instal Twurl. Twurl is a curl-like application, tailored specifically for the Twitter API.

    1. Install twurl in your system. $ sudo gem install twurl
    2. Set authorization to twurl acceess your twitter APP. $ twurl authorize --consumer-key xxxxx --consumer-secret xxxxx
    3. That is the output for the prevoius command: Go to https://api.twitter.com/oauth/authorize?oauth_consumer_key=xxxx&oauth_nonce=ffff&oauth_signature=bbb&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1556889574&oauth_token=ddd&oauth_version=1.0 and paste in the supplied PIN
    4. Open the browser copy and paste the provided URL https://api. .... version=1.0
    5. You will be redirected to a page asking to confirm the authorization. Confirm it.
    6. You will receive a message: 'You've granted access to APP_Name! Next, return to APP_Name and enter this PIN to complete the authorization process. PIN = 09010101'.
    7. Just copy the PIN number and paste back in the terminal and hit enter.
    8. You will get a message in the terminal Authorization successful.
    9. Go to yor APP_Name page https://developer.twitter.com/en/apps/123456 and go to Keys and tokens section. You need to regenerate the Access token & access token secret. Hit the button 'regenerate'
    10. Once it is regenerate you can get access to the api trough twurl in your terminal: $ twurl -H "ads-api.twitter.com" "/5/accounts". Please note that today (May-2019) I am using number 5 in "/5/accounts". You must to check your version at your date.
    11. Now you can get access to the Twitter Ads API trough curl in php also.
    12. Create a class TwitterAPIExchangeService (I am in Laravel 5.8). You can get the class in this post.

    Use the follow code with your keys:

    $settings = array(
        'oauth_access_token'        => env('TWITTER_ACCESS_TOKEN'),
        'oauth_access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
        'consumer_key'              => env('TWITTER_CONSUMER_KEY'),
        'consumer_secret'           => env('TWITTER_CONSUMER_SECRET'),
    );
    
    //Regular Twitter API
    $url = 'https://api.twitter.com/1.1/followers/ids.json';
    $getfield = '?screen_name=J7mbo';
    
    //Ads Twitter API
    //$url = 'https://ads-api.twitter.com/5/accounts';
    //$getfield = '';
    
    
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchangeService($settings);
    $data = $twitter->setGetfield($getfield)
        ->buildOauth($url, $requestMethod)
        ->performRequest();
    dd($data);
    

  2. Need to regenerate keys and tokens. after that its work for me

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search