Saturday, December 11, 2010

Uploading to Freebase, part II: authenticating with OAuth

I'd hoped to have written the bulk of human knowledge to Freebase by now, but I came to a screeching halt when I found that I'd need cookies and sessions and such.

That is, you have to authenticate to write data in bulk to Freebase. Here's one way to do so using OAuth and PHP.

1. Sign in to Freebase and register an app. Take note of your Consumer Key and Consumer Secret.

2. Get oauth-php and add it to a directory where your code can see it.

3. On the page from which you'd like users to authenticate, include the following code (adapted pretty directly from the Twitter example):

require "oauth-php/library/OAuthStore.php";
require "oauth-php/library/OAuthRequester.php";

/**
* oauth-php: Example OAuth client
*
* Performs simple 2-legged authentication
*
* The MIT License
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

// register at http://www.freebase.com/apps/create and fill these two
define("FREEBASE_CONSUMER_KEY", "FILL IN");
define("FREEBASE_CONSUMER_SECRET", "FILL IN");

define("FREEBASE_OAUTH_HOST","https://api.freebase.com");
define("FREEBASE_REQUEST_TOKEN_URL", FREEBASE_OAUTH_HOST . "/api/oauth/request_token");
define("FREEBASE_AUTHORIZE_URL", "https://www.freebase.com/signin/authorize_token");
define("FREEBASE_ACCESS_TOKEN_URL", FREEBASE_OAUTH_HOST . "/api/oauth/access_token");

define('OAUTH_TMP_DIR', function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV["TMP"]));

// test
$options = array('consumer_key' => FREEBASE_CONSUMER_KEY, 'consumer_secret' => FREEBASE_CONSUMER_SECRET);
OAuthStore::instance("2Leg", $options);

try
{
// Obtain a request object for the request we want to make
$request = new OAuthRequester(FREEBASE_REQUEST_TOKEN_URL, "GET");
$result = $request->doRequest(0);
parse_str($result['body'], $params);

echo $result['body'];

}
catch(OAuthException2 $e)
{
echo "Exception" . $e->getMessage();
}

?>


When you load that code, you should see a token, good for at least one POST to Freebase. (I hope -- I'm writing this up as I go.)

Please stay tuned for the next exciting installment of Uploading to Freebase!

No comments: