简体   繁体   中英

Unable to obtain token using Abraham's TwitterOAuth for PHP; HTTP 500 returned

I am attempting to use @abraham's TwitterOAuth 0.5.3 library for PHP, but when I make a request to request a token for the 3-legged authorization , I receive an HTTP 500 as a response.

Here is how I have the code set up in PHP:

<?php

/* Start session and load library. */
session_start();

require_once('config.php');

require_once('twitteroauth/autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;

/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

/* Get temporary credentials. */
// Error occurs on the following line, unable to dump $request_token 
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));

//print_r($request_token); // <-- Never reached!!

I know that this problem is not within the Twitter API, as I have verified that I can access my Twitter account via the Dev console .

In addition, I have verified to some degree that the TwiterOAuth library is working, by following the Authorization flow example provided with the library. The example can also access my Twitter account.

I just can't figure out what is going on as I am unable to properly authorize my PHP application to have access to my Twitter account.

What am I doing wrong?

It turns out that a response was never obtained. As a result, attempting to process a response, when there was none resulted in errors on the server side.

One of the PHP functions that Twitter OAuth relies upon is curl. I had tested to see if curl_init existed:

print function_exists('curl_init') ? 'curl_init is enabled' : 'curl_init is disabled';

and I erroneously assumed that curl_exec was also enabled. (Why would you leave curl_init enabled, but only disable curl_exec ?)

That assumption was incorrect as my web hosting provider has disabled curl_exec "due to security concerns" and I was unaware of this. In addition, my call to use the Twitter API has worked in the past, so this was new behavior.

It took me a while to come back to testing curl_exec . I verified that I was receiving a valid TwitterOauth object and eventually wound my way into the TwitterOauth class and into the request function.

I was receiving no curl error, but was the response from curl_exec was null (not TRUE or FALSE as expected ). I thought that this was unusual and at first thought that curl was missing a configuration option.

However, it was not.

So, if you run into problems with this library (which has worked great for me in the past), it may be that your hosting provider disabled curl_exec .

You can test this scenario via the following PHP code:

print function_exists('curl_exec') ? 'curl_exec is enabled' : 'curl_exec is disabled';

My problem was fixed in another way. After checking (as per jhenderson2099 answer) that my hosting had curl_exec enabled (which it did). I found out that my problem was caused by two lines in src/TwitterOauth.php (TwitterOauth class):

$bundlePath = CaBundle::getSystemCaRootBundlePath(); <-- Comment this line
$options = [
        // CURLOPT_VERBOSE => true,
        CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
        CURLOPT_HEADER => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_TIMEOUT => $this->timeout,
        CURLOPT_USERAGENT => $this->userAgent,
        $this->curlCaOpt($bundlePath) => $bundlePath,<-- Comment this line
    ];

so that your code will look like this:

//$bundlePath = CaBundle::getSystemCaRootBundlePath();
        $options = [
            // CURLOPT_VERBOSE => true,
            CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout,
            CURLOPT_HEADER => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYHOST => 2,
            CURLOPT_SSL_VERIFYPEER => true,
            CURLOPT_TIMEOUT => $this->timeout,
            CURLOPT_USERAGENT => $this->userAgent,
            //$this->curlCaOpt($bundlePath) => $bundlePath,
        ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM