4

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.

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 \Dropbox\OAuth\Storage\Encrypter('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

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

    $storage = new \Dropbox\OAuth\Storage\Session($encrypter);

    $OAuth = new \Dropbox\OAuth\Consumer\Curl($key, $secret, $storage, $callback);

    $dropbox = new \Dropbox\API($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');
?>

2 Answers 2

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 \Dropbox\OAuth\Storage\Session($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.

6
  • Thanks, I have updated my question and the code. I am now supposed to be using the DB to store my token. It works outside of Wordpress, but not inside. Ideas?
    – emd
    Commented Nov 17, 2012 at 6:07
  • You'll have to store the token in the WP database and then add some code to check that it exists and retrieve it. This may require serializing and unserializing the token.
    – totels
    Commented Nov 17, 2012 at 17:11
  • Ya, that's what this Framework is supposed to do. Really frustrated that it works outside of Wordpress (table is created and token is stored) but not when run as a plugin (table not created). Why won't it create the table? I guess I'll have to modify the framework to expose the token and set it. Sigh
    – emd
    Commented Nov 17, 2012 at 17:47
  • Well I have no idea why it wasn't working before, but I got it to work by writing my own Storage class that saved the token in the WP Options table.
    – emd
    Commented Nov 18, 2012 at 0:28
  • Heh, yes, that's what I said in my first sentence. ;)
    – totels
    Commented Nov 19, 2012 at 23:40
0

I ended up writing my own Storage class that used the WordPress Options table to store the encrypted token.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.