function du_oauth_device($appkey, $appsec) { $device_para = 'client_id=' . $appkey . '&response_type=device_code&scope=basic,netdisk'; $device_json = do_api('https://openapi.baidu.com/oauth/2.0/device/code', $device_para); $device_array = json_decode($device_json, 1); oaerr($device_array); echo <<<EOF Launch your favorite web browser and visit {$device_array['verification_url']} Input {$device_array['user_code']} as the user code if asked. After granting access to the application, come back here and press Enter to continue. EOF; getline(); for (;;) { //一个死循环 $token_para = 'grant_type=device_token&code=' . $device_array['device_code'] . '&client_id=' . $appkey . '&client_secret=' . $appsec; $token_json = do_api('https://openapi.baidu.com/oauth/2.0/token', $token_para); $token_array = json_decode($token_json, 1); if (oaerr($token_array, 0)) { break; } else { echon('Authentication failed. Please check the error message and try again.'); echo <<<EOF Launch your favorite web browser and visit {$device_array['verification_url']} Input {$device_array['user_code']} as the user code if asked. After granting access to the application, come back here and press Enter to continue. EOF; continueornot(); continue; } break; } $access_token = $token_array['access_token']; $refresh_token = $token_array['refresh_token']; return array('access_token' => $access_token, 'refresh_token' => $refresh_token); }
===========================Baidu PCS Uploader=========================== Usage: {$argv['0']} init|quickinit|quota Usage: {$argv['0']} upload|download path_local path_remote Usage: {$argv['0']} delete path_remote Usage: {$argv['0']} uploadbig path_local path_remote [slice_size(default:1073741824)] [temp_dir(def:/tmp/)] Usage: {$argv['0']} fetch path_remote path_to_fetch ======================================================================== EOF; if (!is_dir(CONFIG_DIR)) { mkdir(CONFIG_DIR); } if (!is_file(CONFIG_DIR . '/config.lock') || $argv[1] == 'init' || $argv[1] == 'quickinit') { //进行初始化 echon('Uploader initialization will be begin. If you have already configured the uploader before, your old settings will be overwritten.'); continueornot(); du_init($argv[1] == 'quickinit'); file_put_contents(CONFIG_DIR . '/config.lock', time()); die; } $access_token = file_get_contents(CONFIG_DIR . '/access_token'); $refresh_token = file_get_contents(CONFIG_DIR . '/refresh_token'); if ($refresh_token) { //若存在refresh token,则刷新它。 $token_array = do_oauth_refresh(file_get_contents(CONFIG_DIR . '/appkey'), file_get_contents(CONFIG_DIR . '/appsec'), file_get_contents(CONFIG_DIR . '/refresh_token')); if ($token_array['access_token'] && $token_array['refresh_token']) { //防止获取不到token而自杀的行为 $access_token = $token_array['access_token']; $refresh_token = $token_array['refresh_token']; file_put_contents(CONFIG_DIR . '/access_token', $access_token); file_put_contents(CONFIG_DIR . '/refresh_token', $refresh_token);