Twitter

From DreamFactory
Jump to: navigation, search
(Created page with "The Twitter API requires OAuth authentication, but because we're going to interact with the API programmatically rather than on behalf of an authenticated user we'll need to t...")
(No difference)

Revision as of 21:01, 23 July 2018

The Twitter API requires OAuth authentication, but because we're going to interact with the API programmatically rather than on behalf of an authenticated user we'll need to take advantage of a third-party library to handle the OAuth logic. For PHP we recommend TwitterOAuth (https://github.com/abraham/twitteroauth), created and maintained by Abraham Williams.

To install the package, navigate to your DreamFactory root directory and execute this command:

$ composer require abraham/twitteroauth --no-dev

With that done, it's time to configure the service. Login to your DreamFactory instance as an administrator and follow these steps:

  1. Select Services > Create > Script
  2. Choose one of the four supported scripting engines (Node.js, PHP, Python, V8)
  3. On the Info tab, enter a name, label, and description. Remember, the name will constitute part of the service URL, so be sure to enter something meaningful such as twitter.
  4. Click the Config tab.
$consumerKey    = YOUR_CONSUMER_KEY;
$consumerSecret = YOUR_CONSUMER_SECRET;
$oauthToken     = YOUR_OAUTH_TOKEN; 
$oauthSecret    = YOUR_OAUTH_SECRET;

$connection = new \Abraham\TwitterOAuth\TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthSecret);

if ($event['request']['method'] == "GET") {

    $response = $connection->get("statuses/home_timeline", ["count" => 10, "exclude_replies" => true]);

} elseif ($event['request']['method'] == "POST") {

    $response = $connection->post("statuses/update", ["status" => "hello world"]);

}

return json_encode(["response" => $response]);