Storing Dropbox Authentication?

I am trying to create a WordPress plugin that exports each blog post to my dropbox folder. The code is below but I have a problem.

If I run this code outside WordPress, it works perfectly. The table is created and my token is stored. If I use a different browser it works perfectly…no authentication because it reads from the DB.

Read More

My problem is that when I put this into my plugin, the table is not created at all. So if I go to a different browser, I have to re-authenticate.

Help please.

Code:

/*
 * Copyright 2012 Erin Dalzell.
 *
 */

require_once('Dropbox/API.php');
require_once('Dropbox/Exception.php');
require_once('Dropbox/OAuth/Consumer/ConsumerAbstract.php');
require_once('Dropbox/OAuth/Consumer/Curl.php');
require_once('Dropbox/OAuth/Storage/Encrypter.php');
require_once('Dropbox/OAuth/Storage/StorageInterface.php');
require_once('Dropbox/OAuth/Storage/Session.php');
require_once('Dropbox/OAuth/Storage/Filesystem.php');
require_once('Dropbox/OAuth/Storage/PDO.php');

include ABSPATH . '/wp-config.php';

function etd_initialize() {

global $current_user;

    // Set your consumer key, secret and callback URL
    // should be in the settings
    $key      = 'xxxxx';
    $secret   = 'yyyyy';
    $current_user = wp_get_current_user();

    // Check whether to use HTTPS and set the callback URL
    $protocol = (!empty($_SERVER['HTTPS'])) ? 'https' : 'http';
    $callback = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];


    // Instantiate the Encrypter and storage objects
    $encrypter = new DropboxOAuthStorageEncrypter('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

    // Instantiate the database data store and connect
    $storage = new DropboxOAuthStoragePDO($encrypter, 1);
    $storage->connect(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

    $storage = new DropboxOAuthStorageSession($encrypter);

    $OAuth = new DropboxOAuthConsumerCurl($key, $secret, $storage, $callback);

    $dropbox = new DropboxAPI($OAuth);

    return $dropbox;

}

function export_to_dropbox($id) {
    $dropbox = etd_initialize();

    $post = get_post($id);


    // Create a temporary file and write some data to it
    $tmp = tempnam('/tmp','dropbox');
    file_put_contents($tmp, $post->post_content);

    // Upload the file with an alternative filename
    $put = $dropbox->putFile($tmp, $post->post_title . '.md');

    // Unlink the temporary file
    unlink($tmp);
}

register_activation_hook( __FILE__, 'etd_initialize' );
add_action('publish_post', 'export_to_dropbox');
?>

Related posts

Leave a Reply

2 comments

  1. I’m not familiar with the Dropbox API or their libraries, but most likely you’ll need to write your own session handler to store the session data in your WP DB (or wherever you want, safely) and associate it with your user account. Essentially adusting this line: $storage = new DropboxOAuthStorageSession($encrypter);.

    EDIT:

    If it’s the WordPress side of things you are trying to figure out, you’ll need to save your OAuth tokens in a usermeta field, then instead of doing the full authentication flow, first check that there isn’t a usermeta value saved and use it if there is.