0

I'm trying to integrate the twitter api [for searching] into a modx site using this handy article:Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

I setup a static file that references the api and it worked perfectly, like a charm.

I integrate it as a modx extension & all I get returned is:

{"errors":[{"message":"Could not authenticate you","code":32}]} 

I've dumped the curlopts for each example and they are identical:

Array
(
    [10023] => Array
        (
            [0] => Authorization: OAuth oauth_consumer_key="xxx", 
                oauth_nonce="xxx", 
                oauth_signature_method="HMAC-SHA1", 
                oauth_token="xxx", 
                oauth_timestamp="111111111111", 
                oauth_version="1.0", 
                q="%2523BOACanada", 
                count="3", 
                oauth_signature="xxx"
            [1] => Expect:
        )

    [42] => 
    [10002] => https://api.twitter.com/1.1/search/tweets.json?q=#BOACanada&count=3
    [19913] => 1
    [13] => 10
)

Like I say this is modx so I am calling the component like:

[[!TwitterExchange? &getfield=`#BOACanada` &count=`3`]]

The TwitterExchange snippet:

<?php
$output = '';

$exchange_core = $modx->getOption('core_path').'components/TwitterAPIExchange/model/';

$scriptProperties['oauth_access_token'] = "xxxxx";
$scriptProperties['oauth_access_token_secret'] = "xxxxx";
$scriptProperties['consumer_key'] = "xxxxx";
$scriptProperties['consumer_secret'] = "xxxxx";
$scriptProperties['url'] = 'https://api.twitter.com/1.1/search/tweets.json';


$exchange = $modx->getService('twitterapiexchange', 'TwitterAPIExchange', $exchange_core, $scriptProperties);

if (!$exchange instanceof TwitterAPIExchange){

    return 'Insantiation failed';

}

$output = $exchange->searchTweets();

return $output;

And finally, the class. I have removed anything that was not modified from the original class:

<?php

class TwitterAPIExchange
{
    private $oauth_access_token;
    private $oauth_access_token_secret;
    private $consumer_key;
    private $consumer_secret;
    private $postfields;
    private $getfield;
    protected $oauth;
    public $url;

    public function __construct(modX &$modx,array $scriptProperties){

        $this->modx =& $modx;

        $this->modx->setLogLevel(xPDO::LOG_LEVEL_ERROR);

        $this->modx->setLogTarget('ECHO');

        $this->scriptProperties = $scriptProperties;

        if (!in_array('curl', get_loaded_extensions())) 
        {
        throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
        }

        if (!isset($scriptProperties['oauth_access_token'])
            || !isset($scriptProperties['oauth_access_token_secret'])
            || !isset($scriptProperties['consumer_key'])
            || !isset($scriptProperties['consumer_secret']))
        {
            throw new Exception('Make sure you are passing in the correct parameters');
        }

        $this->oauth_access_token = $scriptProperties['oauth_access_token'];
        $this->oauth_access_token_secret = $scriptProperties['oauth_access_token_secret'];
        $this->consumer_key = $scriptProperties['consumer_key'];
        $this->consumer_secret = $scriptProperties['consumer_secret'];

    }




    public function searchTweets(){

        $url = $this->scriptProperties['url'];

        $getfield = "?q=".$this->scriptProperties['getfield']."&count=".$this->scriptProperties['count']."";
        $requestMethod = 'GET';     
        $qstring = $this->setGetfield($getfield);
        $oath = $this->buildOauth($url, $requestMethod, $qstring);      
        $json = $this->performRequest($getfield, $oath['url'], $oath['oauth']); 


        echo $json;



    }


    public function setGetfield($string)
    {

        $search = array('#', ',', '+', ':');
        $replace = array('%23', '%2C', '%2B', '%3A');
        $string = str_replace($search, $replace, $string);  

        return $string;
    }


    public function buildOauth($url, $requestMethod, $qstring)
    {
        if (!in_array(strtolower($requestMethod), array('post', 'get')))
        {
            throw new Exception('Request method must be either POST or GET');
        }

        $consumer_key = $this->consumer_key;
        $consumer_secret = $this->consumer_secret;
        $oauth_access_token = $this->oauth_access_token;
        $oauth_access_token_secret = $this->oauth_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'
        );

        $getfield = $qstring;

        if (!is_null($getfield))
        {
            $getfields = str_replace('?', '', explode('&', $getfield));
            foreach ($getfields as $g)
            {
                $split = explode('=', $g);
                $oauth[$split[0]] = $split[1];
            }
        }

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

       $output = array();
        $output['url'] = $url;
        $output['oauth'] = $oauth;

        return $output;
    }


    public function performRequest($qstring, $url, $oauth){

        $header = array($this->buildAuthorizationHeader($oauth), 'Expect:');

        $getfield = $qstring;

        $options = array( 
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_HEADER => false,
            CURLOPT_URL => $url.$getfield,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 10,
            );

        echo'<pre>';print_r($options);echo'</pre>';

        $feed = curl_init();
        curl_setopt_array($feed, $options);
        $json = curl_exec($feed);
        curl_close($feed);

        return $json; 

    }


}

Why am I getting an authentication error?

Community
  • 1
  • 1
Sean Kimball
  • 4,506
  • 9
  • 42
  • 73

0 Answers0