Frankie Healy
Frankie Healy

Reputation: 43

Box.net API V2 PHP curl 401 unauthorized response

I'm currently integrating Box.net into a website and have come stuck when trying to access the operations after authentication.

I'm authenticating fine and retrieving my token, however regardless of the token being active every response I receive is a 401 unauthorized.

My testing code:

$header = array('Authorization: BoxAuth api_key='.$apikey.'&auth_token='.$auth);

$curl = curl_init();

curl_setopt( $curl, CURLOPT_URL, 'https://api.box.com/2.0/folders/0');
curl_setopt( $curl, CURLOPT_HTTPHEADER, $header);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $curl, CURLOPT_HTTP_VERSION, "CURL_HTTP_VERSION_1_1");
curl_setopt( $curl, CURLOPT_VERBOSE, true );
//curl_setopt( $curl, CURLINFO_HEADER, true);
curl_setopt( $curl, CURLINFO_HEADER_OUT, true);
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2);

$res = curl_exec( $curl );

if ($res === false)
{
    print_r('Curl error: ' . curl_error($curl));
}

var_dump( curl_getinfo($curl) );
curl_close($curl);

var_dump($res);

Request Dump:

array(22) {
  ["url"]=>
  string(33) "https://api.box.com/2.0/folders/0"
  ["content_type"]=>
  string(16) "application/json"
  ["http_code"]=>
  int(401)
  ["header_size"]=>
  int(203)
  ["request_size"]=>
  int(172)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(0.783732)
  ["namelookup_time"]=>
  float(0.005711)
  ["connect_time"]=>
  float(0.15543)
  ["pretransfer_time"]=>
  float(0.479779)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(171)
  ["speed_download"]=>
  float(218)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(171)
  ["upload_content_length"]=>
  float(0)
  ["starttransfer_time"]=>
  float(0.783576)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
  ["request_header"]=>
  string(172) "GET /2.0/folders/0 HTTP/1.1
Host: api.box.com
Accept: */*
Authorization: BoxAuth api_key=xxxxxxxxxxxxxx&auth_token=xxxxxxxxxxxxxxx

"
}

Response:

string(171) "{"type":"error","status":401,"code":"unauthorized","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Unauthorized","request_id":"xxxxxxxxxxxxxx"}"

For reference, I have tried the v1 api (just to see if I had any joy) via the Box_Rest_Client written by Sean Rose and that worked fine for the basic operations.

Any help/pointers would be much appreciated.

Thanks

Frankie

Upvotes: 1

Views: 1857

Answers (1)

John Hoerr
John Hoerr

Reputation: 8025

Frankie, the BoxAuth authorization mechanism provides the V2 API backwards-compatibility with V1 tokens.

Authorization: BoxAuth api_key=API_KEY&auth_token=V1_TOKEN

Authorization: Bearer V2_TOKEN

Try using a V1-issued token with the BoxAuth header in your V2 API request and it should work.

Upvotes: 1

Related Questions