PHP Classes

Box API

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How to Implement a PH...  >  All threads  >  Box API  >  (Un) Subscribe thread alerts  
Subject:Box API
Summary:Using this library to hook up Box API v2
Messages:5
Author:Hank Mack
Date:2013-01-08 05:59:35
Update:2013-01-08 06:52:52
 

  1. Box API   Reply   Report abuse  
Picture of Hank Mack Hank Mack - 2013-01-08 05:59:36
Hi Manuel, many thanks for this OAuth library. I'd like to use it to hook up the Box API. I'm having trouble, however, setting up Box as an additional server. I'd very much appreciate any insight on the matter. My "login with Box" page, https://mysite.com/box/login_with_box, successfully redirects the user to Box's authentication page. My problem occurs right after the user enters a valid username and password and grants access to my Box app. The results vary according to my redirect_uri and the url of my login page where I've put my client_id and client_secret: 1) If my redirect_uri matches my https://mysite.com/login_with_box, the user redirects to that same url, obviously, which in turn sends the user back to Box's authorization page; and 2) if my redirect_uri differs from https://mysite.com/login_with_box page, then the user successfully returns to my redirect_uri, the url of which includes Box's 30-second code. I can't figure out how to show the user's folders, files, info or whatever else. Many thanks for your consideration. Here's where I stand:

I've added the following case to oauth_client.php.

case 'Box':
$this->oauth_version = '2.0';
$this->request_token_url = '';
$this->dialog_url = 'https://api.box.com/oauth2/authorize?response_type=code&client_id={CLIENT_ID}&state=authenticated';
$this->append_state_to_redirect_uri = '';
$this->access_token_url = 'https://api.box.com/oauth2/token';
$this->authorization_header = true;
$this->url_parameters = false;
break;

And the important part of my login_with_box.php, which includes my Box app's client_id and client_secret, reads as follows.

if(($success = $client->Initialize())) {
if(($success = $client->Process())) {
if(strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.box.com/2.0/users/me',
'GET', array('format'=>'json'), array('FailOnAccessError'=>true), $user);
}
}
$success = $client->Finalize($success);
}

What am I missing here? Many thanks for your consideration.

  2. Re: Box API   Reply   Report abuse  
Picture of Hank Mack Hank Mack - 2013-01-08 06:49:11 - In reply to message 1 from Hank Mack
I figured it out. The problem of course was entirely my fault. Here's how I hooked up the Box API v2 with Manuel's finely crafted library:

I added the following case to https://mysite.com/oauth/oauth_client.php.

case 'Box':
$this->oauth_version = '2.0';
$this->request_token_url = '';
$this->dialog_url = 'https://api.box.com/oauth2/authorize?response_type=code&client_id={CLIENT_ID}&state={STATE}';
$this->append_state_to_redirect_uri = '';
$this->access_token_url = 'https://api.box.com/oauth2/token';
$this->authorization_header = true;
$this->url_parameters = false;
break;

I created an app on developers.box.com and set the required redirect_uri to https://mysite.com/oauth/login_with_box.php.

My login_with_box.php reads as follows.

<?php require('http.php'); require('oauth_client.php');

$client = new oauth_client_class;
$client->server = 'Box';
$client->redirect_uri = 'https://mysite.com/box/login_with_box.php';

// Box API
$client->client_id = 'xxxxxx_BOX_API_CLIENT_ID_xxxxxx';
$client->client_secret = 'xxxxxx_BOX_API_CLIENT_SECRET_xxxxxx';

if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('You need an app to do that.');

if(($success = $client->Initialize())) {
if(($success = $client->Process())) {
if(strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.box.com/2.0/folders/0',
'GET', array('format'=>'json'), array('FailOnAccessError'=>true), $folder);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;

if($success) { ?><!doctype html>
<html>
<head>
<title>Box OAuth client results</title>
</head>
<body>
<?php echo '<h1>You successfully logged in with Box</h1>'; echo '<pre>', HtmlSpecialChars(print_r($folder, 1)), '</pre>'; ?>

</body>
</html><?php } else { ?><!doctype html>
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html><?php } ?>

I hope this helps somebody.

  3. Re: Box API   Reply   Report abuse  
Picture of Hank Mack Hank Mack - 2013-01-08 06:50:35 - In reply to message 1 from Hank Mack
I figured it out. The problem of course was entirely my fault. Here's how I hooked up the Box API v2 with Manuel's finely crafted library:

I added the following case to https://mysite.com/oauth/oauth_client.php.

case 'Box':
$this->oauth_version = '2.0';
$this->request_token_url = '';
$this->dialog_url = 'https://api.box.com/oauth2/authorize?response_type=code&client_id={CLIENT_ID}&state={STATE}';
$this->append_state_to_redirect_uri = '';
$this->access_token_url = 'https://api.box.com/oauth2/token';
$this->authorization_header = true;
$this->url_parameters = false;
break;

I created an app on developers.box.com and set the required redirect_uri to https://mysite.com/oauth/login_with_box.php.

My login_with_box.php reads as follows.

<?php require('http.php'); require('oauth_client.php');

$client = new oauth_client_class;
$client->server = 'Box';
$client->redirect_uri = 'https://mysite.com/oauth/login_with_box.php';

// Box API
$client->client_id = 'xxxxxx_BOX_API_CLIENT_ID_xxxxxx';
$client->client_secret = 'xxxxxx_BOX_API_CLIENT_SECRET_xxxxxx';

if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('You need an app to do that.');

if(($success = $client->Initialize())) {
if(($success = $client->Process())) {
if(strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.box.com/2.0/folders/0',
'GET', array('format'=>'json'), array('FailOnAccessError'=>true), $folder);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;

if($success) { ?><!doctype html>
<html>
<head>
<title>Box OAuth client results</title>
</head>
<body>
<?php echo '<h1>You successfully logged in with Box</h1>'; echo '<pre>', HtmlSpecialChars(print_r($folder, 1)), '</pre>'; ?>

</body>
</html><?php } else { ?><!doctype html>
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html><?php } ?>

I hope this helps somebody.

  4. Re: Box API   Reply   Report abuse  
Picture of Hank Mack Hank Mack - 2013-01-08 06:51:34 - In reply to message 1 from Hank Mack
Scratch this one, which includes the wrong redirect_uri. The following code is correct.

  5. Re: Box API   Reply   Report abuse  
Picture of Hank Mack Hank Mack - 2013-01-08 06:52:52 - In reply to message 4 from Hank Mack
CORRECT:

I figured it out. The problem of course was entirely my fault. Here's how I hooked up the Box API v2 with Manuel's finely crafted library:

I added the following case to https://mysite.com/oauth/oauth_client.php.

case 'Box':
$this->oauth_version = '2.0';
$this->request_token_url = '';
$this->dialog_url = 'https://api.box.com/oauth2/authorize?response_type=code&client_id={CLIENT_ID}&state={STATE}';
$this->append_state_to_redirect_uri = '';
$this->access_token_url = 'https://api.box.com/oauth2/token';
$this->authorization_header = true;
$this->url_parameters = false;
break;

I created an app on developers.box.com and set the required redirect_uri to https://mysite.com/oauth/login_with_box.php.

My login_with_box.php reads as follows.

<?php require('http.php'); require('oauth_client.php');

$client = new oauth_client_class;
$client->server = 'Box';
$client->redirect_uri = 'https://mysite.com/oauth/login_with_box.php';

// Box API
$client->client_id = 'xxxxxx_BOX_API_CLIENT_ID_xxxxxx';
$client->client_secret = 'xxxxxx_BOX_API_CLIENT_SECRET_xxxxxx';

if(strlen($client->client_id) == 0
|| strlen($client->client_secret) == 0)
die('You need an app to do that.');

if(($success = $client->Initialize())) {
if(($success = $client->Process())) {
if(strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.box.com/2.0/folders/0',
'GET', array('format'=>'json'), array('FailOnAccessError'=>true), $folder);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
exit;

if($success) { ?><!doctype html>
<html>
<head>
<title>Box OAuth client results</title>
</head>
<body>
<?php echo '<h1>You successfully logged in with Box</h1>'; echo '<pre>', HtmlSpecialChars(print_r($folder, 1)), '</pre>'; ?>

</body>
</html><?php } else { ?><!doctype html>
<html>
<head>
<title>OAuth client error</title>
</head>
<body>
<h1>OAuth client error</h1>
<pre>Error: <?php echo HtmlSpecialChars($client->error); ?></pre>
</body>
</html><?php } ?>

I hope this helps somebody.