dropbox php read file content

1.6k views Asked by At

I am creating a simple dropbox app where after authenticating the user, the app is supposed to prompt for some information, read the content of a txt file. Use ajax for everything after authentication. so far I have managed to authenticate but when I send the info to the server using ajax I am failing to open the file. I have the following server side script:

<?php
require_once "../app/start.php";

require_once __DIR__.'/../app/config.php';

$regex = $_POST['regex'];
(...)


$accessToken = $_SESSION['accessToken'] ;
$client = new Dropbox\Client($accessToken, $GLOBALS['app_name'], 'UTF-8');
var_dump($client);
// Read File todo.txt

$filename = __DIR__.'/todo/todo.txt';
$f = fopen($filename, "w+b") or die("Unable to open file!");
$fileMetadata = $client->getFile('/todo/todo.txt', $f);
var_dump($fileMetadata);

and when I execute it the output is the following:

object(Dropbox\Client)#8 (6) {
  ["accessToken":"Dropbox\Client":private]=>
  string(64) "xxxxxxxxxxxxxxxxxxxxxxxxx"
  ["clientIdentifier":"Dropbox\Client":private]=>
  string(13) "oauth-php/1.0"
  ["userLocale":"Dropbox\Client":private]=>
  string(5) "UTF-8"
  ["apiHost":"Dropbox\Client":private]=>
  string(15) "api.dropbox.com"
  ["contentHost":"Dropbox\Client":private]=>
  string(23) "api-content.dropbox.com"
  ["host"]=>
  object(Dropbox\Host)#5 (3) {
    ["api":"Dropbox\Host":private]=>
    string(15) "api.dropbox.com"
    ["content":"Dropbox\Host":private]=>
    string(23) "api-content.dropbox.com"
    ["web":"Dropbox\Host":private]=>
    string(15) "www.dropbox.com"
  }
}
NULL

Where the last NULL refers to the content of $fileMetadata. Why is it empty?

the contnet of start.php are the dropbox objects.

<?php
session_start();
require_once __DIR__.'/../vendor/autoload.php';
$key = "XCV...";
$secret = "DFG...";
$GLOBALS['app_name'] = "oauth-php/1.0";
$GLOBALS['redirectURI'] = "https://oauth.dev/dropbox_finish.php";
$GLOBALS['HomeURI'] = "https://oauth.dev";

$appInfo = new Dropbox\AppInfo($key, $secret);
$csrfTokenStore = new Dropbox\ArrayEntryStore($_SESSION, 'dropbox-auth-csrf-token');
$webAuth = new Dropbox\WebAuth($appInfo, $GLOBALS['app_name'], $GLOBALS['redirectURI'], $csrfTokenStore);

Edit: in the local files I have a directory called web, where the files executed are. the there is as well a folder (inside web) called todo and the file todo.txt it is inside. __DIR__ refers to inside the web directory that i have mentioned.

In Dropbox i have a direcotry called todo with todo.txt inside.

Edit2: adding the rest of the code. index.php contains:

<?php 
include_once __DIR__.'/../app/start.php';
include_once __DIR__.'/../app/dropbox_auth.php';

Then dropbox_auth.php has the following code:

$authURL = $webAuth->start();
header("Location: $authURL");

and finally dropbox_finish.php

<?php
//session_start();
require_once "../app/start.php";

try {
   list($accessToken, $userId, $urlState) = $webAuth->finish($_GET);
   assert($urlState === null);  // Since we didn't pass anything in start()
}
catch (Dropbox\WebAuthException_BadRequest $ex) {
   error_log("/dropbox-auth-finish: bad request: " . $ex->getMessage());
   // Respond with an HTTP 400 and display error page...
}
//review
catch (Dropbox\WebAuthException_BadState $ex) {
   // Auth session expired.  Restart the auth process.
   header('Location: '.$GLOBALS['HomeURI']);
}
catch (Dropbox\WebAuthException_Csrf $ex) {
   error_log("/dropbox-auth-finish: CSRF mismatch: " . $ex->getMessage());
   // Respond with HTTP 403 and display error page...
}
catch (Dropbox\WebAuthException_NotApproved $ex) {
   error_log("/dropbox-auth-finish: not approved: " . $ex->getMessage());
}
catch (Dropbox\WebAuthException_Provider $ex) {
   error_log("/dropbox-auth-finish: error redirect from Dropbox: " . $ex->getMessage());
}
catch (Dropbox\Exception $ex) {
   error_log("/dropbox-auth-finish: error communicating with Dropbox API: " . $ex->getMessage());
}

$client = new Dropbox\Client($accessToken, $GLOBALS['app_name'], 'UTF-8');
$_SESSION['accessToken'] = $accessToken;

$_SESSION['client_object'] = $client;

{... here I load an HTML form that uses AJAX send to `insert.php`}
1

There are 1 answers

7
Sebastian Hens On

Sorry I cant write a comment right now.

Try to get the folder/file list by calling $client->getMetadataWithChildren('/todo/') and check if $f is a valid FilePointerRessource :)