Dropbox api createshareableling not working - php

I have some code created, which uploads a sample file and then should give the shareable url back, but it gives an error:
Fatal error: Call to undefined function createShareableLink() in /home/u983866586/public_html/db/dropbox.php on line 52
My code:
# Include the Dropbox SDK libraries
require_once "Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("keys.json");
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
$authorizeUrl = $webAuth->start();
$authCode = "something";
$accessToken= "M4OYlxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
$accountInfo = $dbxClient->getAccountInfo();
//~ print_r($accountInfo);
$f = fopen("working-draft.txt", "rb");
$result = $dbxClient->uploadFile("/working-draft.txt", dbx\WriteMode::add(), $f);
fclose($f);
//~ print_r($result);
$folderMetadata = $dbxClient->getMetadataWithChildren("/");
print_r($folderMetadata);
$f = fopen("working-draft.txt", "w+b");
$fileMetadata = $dbxClient->getFile("/working-draft.txt", $f);
fclose($f);
createShareableLink("/working-draft.txt");
The function createSareableLink is from: http://dropbox.github.io/dropbox-sdk-php/api-docs/v1.1.x/source-class-Dropbox.Client.html#1007-1038
but it is not working.
Arend-Jan

I think you just forgot to use "$dbxClient->" before "createShareableLink".
So that should work for you:
$linkToShare = $dbxClient->createShareableLink("/working-draft.txt");
echo $linkToShare;

Related

Upload files to dropbox with php - Dropbox-SDK-PHP

im a beginer and i traying to upload files to dropbox using dropbox-sdk-php
this is the code that I am using
live test : http://test.dedragon.net
$dropboxKey ='MY_KEY';
$dropboxSecret ='MY_SECRET';
$appName='MY_APPNAME';
$acessToken = "MY_ACCESTOKEN";
set_error_handler('error');
$appInfo = new Dropbox\AppInfo($dropboxKey,$dropboxSecret);
//Store CSRF token
$csrfTokenStore = new Dropbox\ArrayEntryStore($_SESSION['k6'],'dropbox-auth');
//define auth details
$webAuth = new Dropbox\WebAuth($appInfo,$appName,'http://test.dedragon.net',$csrfTokenStore);
$client = new Dropbox\client($acessToken,$appName,'UTF-8');
//time to upload file
if (!empty($_FILES)) {
$nombre = uniqid();
$tempFile = $_FILES['file']['tmp_name'];
$f = fopen($_FILES['file']['tmp_name'], "rb");
$ext = explode(".", $_FILES['file']['name']);
$ext =end($ext);
$nombredropbox = "/". $nombre .'.'.$ext;
$tama = explode(".", $_FILES['file']['size']);
try{
$client->uploadFile($nombredropbox,Dropbox\WriteMode::add(),$f);
}catch(Dropbox\Exception_InvalidAccessToken $e){
error('001',$e);
}
}
function error($numero,$texto){
$ddf = fopen('error.log','a');
fwrite($ddf,"[".date("r")."] Error $numero: $texto\r\n");
fclose($ddf);
}
shows me Status Code: 500
and Error appear
[05-Jun-2019 09:08:13 UTC] PHP Fatal error: Uncaught Dropbox\Exception_BadRequest: HTTP status 400
{"error": "v1_retired"} in /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/RequestUtil.php:250
Stack trace:
#0 /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/Client.php(558): Dropbox\RequestUtil::unexpectedStatus(Object(Dropbox\HttpResponse))
#1 /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/Client.php(423): Dropbox\Client->chunkedUploadStart('%PDF-1.5\r\n%\xB5\xB5\xB5\xB5...')
#2 /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/RequestUtil.php(279): Dropbox\Client->Dropbox\{closure}()
#3 /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/Client.php(424): Dropbox\RequestUtil::runWithRetry(3, Object(Closure))
#4 /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/Client.php(286): Dropbox\Client->_upload in /home/jjddqqslmq4q/public_html/test/terceros/dropbox/vendor/dropbox/dropbox-sdk/lib/Dropbox/RequestUtil.php on line 250
[05-Jun-2019 09:11:52 UTC] PHP Fatal error: Uncaught Dropbox\Exception_BadRequest: HTTP status 400
I changed the SDK to https://github.com/kunalvarma05/dropbox-php-sdk.
<?php
require_once("terceros/dropbox/vendor/autoload.php");
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
$dropboxKey ='MY_KEY';
$dropboxSecret ='MY_SECRET';
$acessToken = "MY_TOKEN";
set_error_handler('error');
$app = new DropboxApp($dropboxKey,$dropboxSecret,$acessToken);
$dropbox = new Dropbox($app);
if (!empty($_FILES)) {
$nombre = uniqid();
$tempFile = $_FILES['file']['tmp_name'];
$ext = explode(".", $_FILES['file']['name']);
$ext =end($ext);
$nombredropbox = "/". $nombre .'.'.$ext;
TRY{
$file = $dropbox->simpleUpload($tempFile,$nombredropbox, ['autorename' => true]);
//Uploaded File
}CATCH(\EXCEPTION $E){
ERROR('001',$E);
}
}
function error($numero,$texto){
$ddf = fopen('error.log','a');
fwrite($ddf,"[".date("r")."] Error $numero: $texto\r\n");
fclose($ddf);
}
?>

Download the file from dropbox?

I would like to download a csv file from my dropbox through php to my server.
I tried to use Dropbox API but I cannot get the positive result. I have downloaded the dropbox, installed it on my server and used one of the examples given:
<?php
# Include the Dropbox SDK libraries
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("app-info.json");
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");
$authorizeUrl = $webAuth->start();
$authCode = \trim(\readline("the_authentication_code_from_dropbox"));
list($accessToken, $dropboxUserId) = $webAuth->finish($authCode);
print "Access Token: " . $accessToken . "\n";
$f = fopen("working-draft.txt", "w+b");
$fileMetadata = $dbxClient->getFile("/working-draft.txt", $f);
fclose($f);
print_r($fileMetadata);
?>
But this code does not retrieve my file :( Any help would be kindly appreciated :)

Uncaught exception 'Exception' with message 'The Dropbox SDK uses 64-bit integers

How can I solve the problem with this error? I am using xampp and php 5.5.6 for my application. My code is as follow:
<?php
`enter code here`# Include the Dropbox SDK libraries
require_once "dropbox-php-sdk-1.1.2/lib/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("app-info.json");
$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";
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
$accountInfo = $dbxClient->getAccountInfo();
print_r($accountInfo);
// uploading files
$f = fopen($_FILES["file"]["name"], "rb");
$result = $dbxClient->uploadFile($_FILES["file"]["name"], dbx\WriteMode::add(), $f);
fclose($f);
print_r($result);`enter code here`
?>
When I run the script on my browser, I get this error.
From the README:
Requirements:
PHP 5.3+, with 64-bit integers.

Uploading files to dropbox with XAMPP PHP

I am trying to upload files to dropbox. I am on Windows and using XAMPP. This is what I have.
# Include the Dropbox SDK libraries
require_once "dropbox-sdk/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("../view/configs/json_config.json");
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "MyApp");
$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";
// $fp=fopen("php://stdin", "r");
// $in=fgets($fp,4094);
// fclose($fp);
define('STDIN',fopen("C://xampp/php/php.exe", "r"));
if (PHP_OS == 'WINNT') {
echo '$ ';
$line = trim(fgets(STDIN)); // reads one line from STDIN
//$line = stream_get_line(STDIN, 1024, PHP_EOL);
//$read = str_replace("\r\n", "", $in);
}
else {
//$line = readline('$ ');
$line = str_replace("\n", "", $in);
}
//$authCode = \trim(\readline("Enter the authorization code here: "));
list($accessToken, $dropboxUserId) = $webAuth->finish($line);
print "Access Token: " . $accessToken . "\n";
$dbxClient = new dbx\Client($accessToken, "MyApp");
$accountInfo = $dbxClient->getAccountInfo();
print_r($accountInfo);
$f = fopen("working-draft.txt", "rb");
$result = $dbxClient->uploadFile("/working-draft.txt", dbx\WriteMode::add(), $f);
fclose($f);
print_r($result);
$folderMetadata = $dbxClient->getMetadataWithChildren("/");
print_r($folderMetadata);
$f = fopen("working-draft.txt", "w+b");
$fileMetadata = $dbxClient->getFile("/working-draft.txt", $f);
fclose($f);
print_r($fileMetadata);
which I found here
Now I am already having problems trying to execute this code as this code originally uses the readline function (or library) which is not supported on windows. I found a substitute that I am trying to use but it doesn't work.
Currently, this is the error I am getting:
"Uncaught exception 'Dropbox\Exception_BadRequest' with message ' in ....\RequestUtil.php on line 251."
I have no idea how to get this working. Has anyone done this successfully on windows using XAMPP?
The error here makes sense... it looks like your code tries to read the contents of php.exe, which doesn't sound like a good idea. :-) That's why the string is "... This program cannot be run in DOS mode."
But if you don't redefine STDIN, $line = trim(fgets(STDIN)) looks reasonable. What happens when you run that?
It's throwing an exception because there is something wrong with your request to DropBox.
To get more debugging information, place all of your DropBox method calls within a try/catch block, for example:
try
{
$authorizeUrl = $webAuth->start();
}
catch (dbx\WebAuthException_BadRequest $e) {
var_dump($e); // Will returned detailed information about the error
}
this should start you on the right track in determining the cause.

Dropbox for Yii

Has anyone any idea how to tie in the dropbox php api http://code.google.com/p/dropbox-php/ in Yii. I downloaded the source files and put in ext.dropbox and then inserted the following code
$dropbox = Yii::getPathOfAlias('ext.dropbox');
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::registerAutoloader(array('Dropbox_autoload','autoload'));
$consumerKey = '***';
$consumerSecret = '***';
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
try {
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
$info = $dropbox->getMetaData('Files');
} catch (Exception $e) {
$error = "error: " . $e->getMessage();
}
spl_autoload_register(array('YiiBase','autoload'));
I get the error Fatal error: Class 'CExceptionEvent' not found in *
I'm not sure about Dropbox specifically, but this is how I included SwiftMailer:
Yii::import('swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('swift.swift_init', true);
where the setPathOfAlias looks like:
Yii::setPathOfAlias('swift', '/var/www/lib');
(I'm using it for other apps, which is why it isn't in the Yii tree. Other libs I keep in the extensions dir and for simple ones, a basic "import" is often enough.)
Try this one:
$dropbox = Yii::getPathOfAlias('ext.dropbox');
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::registerAutoloader(array('Dropbox_autoload','autoload'));
$consumerKey = '***';
$consumerSecret = '***';
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
try {
$oauth = new Dropbox_OAuth_PHP($consumerKey, $consumerSecret);
$dropbox = new Dropbox_API($oauth);
$info = $dropbox->getMetaData('Files');
} catch (Exception $e) {
$error = "error: " . $e->getMessage();
}
spl_autoload_register(array('YiiBase','autoload'));
Yii::import('swift.classes.Swift', true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('swift.swift_init', true);
Yii::setPathOfAlias('swift', '/var/www/lib');
Also there is a API libary for download in this Dropbox Lib
and also a PHP 5.3 SDK for the Dropbox REST API

Categories