phpdropbox-apidropbox-php

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


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());

                }
            }
        } 

Solution

  • I Found solution

    Step 1 : First time do login via Authorization/Login URL and you will get access token and refresh token after Authentication save this refresh token in database or env file. its long lived. (https://github.com/kunalvarma05/dropbox-php-sdk/wiki/Authentication-and-Authorization)

    step 2 : using refresh token generate new access token whenever you need using below code

    public function refreshToken()
        {
            $arr = [];
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://api.dropbox.com/oauth2/token');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=refresh_token&refresh_token=<refresh_token_here>");
            curl_setopt($ch, CURLOPT_USERPWD, '<APP_KEY>'. ':' . '<APP_SECRET>');
            $headers = array();
            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            $result = curl_exec($ch);
            $result_arr = json_decode($result,true);
            
    
            if (curl_errno($ch)) {
                $arr = ['status'=>'error','token'=>null];
            }elseif(isset($result_arr['access_token'])){
                $arr = ['status'=>'okay','token'=>$result_arr['access_token']];
            }
            curl_close($ch);
            return $arr;
        }
    

    Call this function to get new access token