DropBox PHP API checks if user is registered and application is allowed

I use the Dropbox API to read and write data to the App folder in Dropbox. Since I use AJAX to send content to putFile.php and get content from getFile.php, redirecting to DropBox-Login does not work. I changed accountInfo.php a bit so that after authentication you will be redirected to the main page. I want to check on the main page whether the user is registered in Dropbox and the application is authorized (because I want to redirect the user to accountInfo.php if he is not logged in).

Thanks in advance!

+3
source share
1 answer

From what I read in the docs, this should do the trick:

<?php
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;

function isLogged() {
    $dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
    $accountInfo = $dbxClient->getAccountInfo();

    return (empty($accountInfo)) ? false : true;
}

if(!isLogged()) {
    $appInfo = dbx\AppInfo::loadFromJsonFile("INSERT_PATH_TO_JSON_CONFIG_PATH");
    $webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");

    $authorizeUrl = $webAuth->start();

    echo "1. Go to: " . $authorizeUrl . "\n";
    echo "2. Click \"Allow\" (you might have to log in first).\n";
    echo "3. Copy the authorization code.\n";
    $authCode = \trim(\readline("Enter the authorization code here: "));

    list($accessToken, $dropboxUserId) = $webAuth->finish($authCode);
    print "Access Token: " . $accessToken . "\n";
} else {
    //User is logged in
}
?>
+1