AI摘要:文章介绍了获取阿里云盘Token的几种方法:手动获取、通过Shell脚本自动获取以及抓取第三方结果。手动方式需要通过手机登录抓包,自动方式可通过保存为sh文件并定时执行来获取,也可通过PHP输出实现,成功后缓存12小时。
Powered by 部落Bot.

傳統方式手動獲取

手機登錄,抓包。參考:落花分享

通過shell自動獲取(先提供一個有效令牌)

說明:保存為sh檔,cron定時執行

CURRENT_DIR=$(cd $(dirname $0); pwd)
token=$(cat $CURRENT_DIR/cache-tk)
ntoken=$(curl https://api.aliyundrive.com/token/refresh -X POST -H "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36" -H "Rererer:https://www.aliyundrive.com/" -H "Content-Type:application/json" -d '{"refresh_token":"'$token'"}' | sed 's/,/\n/g' | grep refresh_token | cut -d \: -f2 | sed 's/"//g')
if [ ! -n "$ntoken" ]; then
ntoken=$(curl https://auth.aliyundrive.com/v2/account/token -X POST -H "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36" -H "Rererer:https://www.aliyundrive.com/" -H "Content-Type:application/json" -d '{"refresh_token":"'$token'", "grant_type": "refresh_token"}'| sed 's/,/\n/g' | grep refresh_token | cut -d \: -f2 | sed 's/"//g')
fi
echo  -n ${ntoken} > $CURRENT_DIR/cache-tk

另一種是抓取第三方的結果

說明:通過php輸出,獲取成功後緩存12小時

$urls = [
    'http://example.com/first',
    'http://example.com/second',
    'http://example.com/third'
];
$cache_file = 'cache-tk';
if (file_exists($cache_file) && time() - filemtime($cache_file) < 12 * 60 * 60) {
    $response = file_get_contents($cache_file);
    } else {
    $log_enabled = false;
    foreach ($urls as $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36');
        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        
        if ($http_code >= 400 && $http_code < 600) {
            continue;
        }
        
        file_put_contents($cache_file, $response);
        
        if ($log_enabled) {
            echo "URL: $url\n";
            echo "HTTP Code: $http_code\n";
            echo "Response:\n$response\n";
            echo "Log saved to file: log.txt\n";
        }
        break;
    }
}
echo file_get_contents($cache_file);