Engagement/Matrics API on GNIP



Hello Team,

I am implementing the matrics api and for this we have activated account for trial ‘TheBuyersApp’.

Please suggest where i will get the related keys and tokens. How a OAuth token can be generated to use in metrics api.

Can i use the 8weekapp(premium account) keys and tokens to access matrics api from ‘TheBuyersApp’ account on gnip


Hello @8WeekApp,

It looks like we have whitelisted your app ID: 15406347 for use with the Engagement API. You should be able to use your keys and tokens related to that app.

You can find the proper keys at the following link: https://apps.twitter.com/app/15406347/keys


Thanks LeBraat,

This works fine now we want to access the @Storzzzcom tweets impressions. We have validated @Storzzzcom with Oauth 1.0 for @8WeekApp. But still not able to get the tweets data from @Storzzzcom account.

Please help me what exactly i need to do. May be if you can help with some code in PHP.


I’m not a PHP guy unfortunately.

Here is the docs page that addresses this situation.

You will notice that the impression metric can be accessed by all three Engagement API metrics, but requires user context, meaning that your app has to be authorized to access content on behalf of the @storzzzcom account.

To do that, you either have to have them authorize your app by using the Sign in with Twitter process, or you have to receive their access tokens and use Twurl to be authorized to access this informaiton.


Thanks Lebraat,

I have authorized the @Storzzzcom account with @8WeekApp using Oauth1.0 and using Twurl as well and both process went successful.

Still it show error below tweet id: 1037709670960558080 is from @Storzzzcom and i while i use the @8WeekApp enterprise credentials it show below error.

[0] => Forbidden to access tweets: 1037709670960558080

Please have a look what is wrong still


Can you please provide the request that you are using? This will help us investigate this situation.


Hello LeBraat,

I have implemented oauth 1.0 for authentication for @8WeekApp here, http://smartops.co.in/twitterapp/callback.php . And i authenticated another account @storzzzcom with this above URL

now i am using the below code to access the impression data and want to fetch the impression of a tweet made from @storzzzcom

$url = "https://data-api.twitter.com/insights/engagement/totals";
//$url = "https://data-api.twitter.com/insights/engagement/historical";
//$url = "https://data-api.twitter.com/insights/engagement/28hr";

//this token from your twitter app
$consumer_key = "8weekapp COUNSUMER KEY";
$consumer_secret = "8weekapp COUNSUMER SECRET";
$oauth_access_token = "8weekapp ACCESS TOKEN";
$oauth_access_token_secret = "8weekapp ACCESS TOKEN SECRET";

$oauth = array( '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');

$base_info = buildBaseString($url, 'POST', $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;

// this tweet id from authorized users tweet

$arrayPostFields['tweet_ids'] = array('1014746768695840768','1015045078912466945','1013223109783113728','1037709670960558080'); //one tweet id from the 

$arrayPostFields['engagement_types'] = array('impressions','engagements','favorites','retweets','replies');
$arrayPostFields['groupings']['group1']['group_by'] = array('tweet.id','engagement.type');

$postfields = json_encode($arrayPostFields);

function buildBaseString($baseURI, $method, $params) {
    $r = array();
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));

function buildAuthorizationHeader($oauth) {
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;

$header = array(buildAuthorizationHeader($oauth), 'Accept-Encoding: gzip','Content-Type: application/json');

//Accept-Encoding: gzip

$options = array( CURLOPT_HTTPHEADER => $header,
                  CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url ,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

$ch = curl_init();
curl_setopt_array($ch, $options);

$buffer = curl_exec($ch);

 //echo 'errno:' . curl_errno($ch);
 //echo 'error text:' . curl_error($ch);
 //echo "\n";

$twitter_data_json = gzinflate(substr($buffer, 10, -8));
$twitter_data = json_decode($twitter_data_json, true);
echo "<pre>";
print_r($twitter_data);echo "\n";



Were you successful with this call?


It gives the result of tweets ids of @8WeekApp but not giving the result of tweet ids from @storzzzcom.
either i have authenticated already @storzzzcom with @8WeekApp using oauth 1.0


I’ve already answered this on another thread. You can only fetch engagement data for the authenticated account.


I’m closing this thread as a duplicate.