John Davidson

how to refresh access token in dropbox api without login in php

0 comments
Message:


I am using https://github.com/kunalvarma05/dropbox-php-sdk for my php project to upload files at dropbox.


Here I do not required any user to use dropbox its just for internal users so i can upload files at my dropbox.


I Generated access token from Dropbox app and everything working but token is getting expired after some time. I did one time Oauth login to regenerate token but new token also expired after some time.


How can i regenerate token or get long-lived token so at backend, so my script can upload files at dropbox after every new upload by user's?


I am using this simple code


include('dropbox/vendor/autoload.php');
$app = new DropboxApp("client_id", "client_secret", 'access_token');
$dropbox = new Dropbox($app);
$data = []; // here getting list of files from database
if (!$data->isEmpty()) {
foreach ($data as $list) {
$filePath = 'folder_path/'.$list->file_name;
$fileName = $list->file_name;
try {
// Create Dropbox File from Path
$dropboxFile = new DropboxFile($filePath);

// Upload the file to Dropbox
$uploadedFile = $dropbox->upload($dropboxFile, "/folder_name/" . $fileName, ['autorename' => true]);
// File Uploaded
echo $uploadedFile->getPathDisplay();
} catch (DropboxClientException $e) {
print_r($e->getMessage());

}
}
}


My Answer:
'app_key', 'app_secret', 'access_token');

$dropbox = new Dropbox($app);

// Check if access token is expired
if ($app->isAccessTokenExpired()) {
// Refresh access token
$app->refreshAccessToken();

// Save new access token
$newAccessToken = $app->getAccessToken();

// Update access token in your database or configuration file
}

// Use the refreshed access token to upload files
$dropboxFile = $dropbox->upload("local_file_path", "dropbox_file_path");

This code snippet demonstrates how to refresh the access token in the Dropbox API using the Dropbox PHP SDK. It checks if the access token is expired, refreshes it if necessary, and then uses the refreshed access token to upload files to Dropbox. Make sure to save the new access token for future use.

Rate this post

3 of 5 based on 1645 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us