Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropBox PHP-API check if user is logged in and the app is authorized

I am using the Dropbox API for reading and writing data of the App-folder in Dropbox. As I am using AJAX to POST the content to the putFile.php and getting the content from getFile.php the redirection to DropBox-Login doesn't work. I changed the accountInfo.php a bit, that you will be redirected to the main page after authentication. I want to check in the main page, whether the user is logged in at dropbox and the app is authorized (because I want to redirect the user to accountInfo.php, if he is not logged in).

Thanks in advance!

like image 849
Dion Avatar asked Dec 10 '25 02:12

Dion


1 Answers

From what I've 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
}
?>
like image 186
Tanatos Avatar answered Dec 12 '25 16:12

Tanatos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!