欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

上传文件到七牛时,bad token了怎么处理?

程序员文章站 2022-06-04 17:22:58
...
想用七牛实现个离线下载的功能。然后把里面的一些加密算法的函数提了出来。
这个是shell脚本运行的,提示bad token了,不知道我下面的脚本哪里写错了?
谢谢~
php download.php https://p.ssl.qhimg.com/t01d1f1a2ae31e3c3e4.png 111.png

download.php,获取encodeURI、encodedEntryURI和accessToken;

?\n"
$signingStr = "{$path}?{$query}\n";
$sign = hmac_sha1($signingStr, SecretKey);
$encodedSign = urlsafe_base64_encode($sign);
//$encodeAccessKey = urlsafe_base64_encode(AccessKey);
//$accessToken = "{$encodeAccessKey}:{$encodedSign}";
$accessToken = AccessKey.":{$encodedSign}";
echo "AccessToken:\r\n{$accessToken}\r\n\r\n";
$data['scope'] = BUCKET;
$data['deadline'] = time()+3600;//过期时间

$encoded = urlsafe_base64_encode(json_encode($data));
//$signature=hash_hmac('sha1',$encoded,'KEY',true);
$signature = hmac_sha1($encoded, SecretKey);
$encode_signed = urlsafe_base64_encode($signature);
//$UploadToken=$encode_signed.':'.$encoded; 
$UploadToken = AccessKey.":".$encode_signed.":".$encoded;
echo "AccessToken:\r\n{$UploadToken}\r\n\r\n";

echo "UploadURL:\r\nhttp://iovip.qbox.me/fetch/{$encodeURI}/to/{$encodedEntryURI}\r\n\r\n";

再通过脚本

sh download.sh encodeURI encodedEntryURI accessToken

传输数据,结果。。。提示:

{"error":"bad token"}

download.sh

curl -i \
     -o - \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -H "Authorization: QBox $3" \
     -X POST \
     "http://iovip.qbox.me/fetch/$1/to/$2"

已经自行处理完成,最终 PHP 部分(有两处错误):

PS:我发现我提问到最后,基本都是靠自己搞定的啊。不知道是不是大牛不屑这些技术含量低的问题还是???
真的和 sg 没法比啊。。。

";
var_dump($signingStr);

$sign = hmac_sha1($signingStr, SecretKey);
$encodedSign = urlsafe_base64_encode($sign);
var_dump($sign);


$accessToken = AccessKey.":{$encodedSign}";

echo "AccessToken:\r\n{$accessToken}\r\n\r\n";

echo "UploadURL:\r\nhttp://iovip.qbox.me/fetch/{$encodeURI}/to/{$encodedEntryURI}\r\n\r\n";

回复内容:

想用七牛实现个离线下载的功能。然后把里面的一些加密算法的函数提了出来。
这个是shell脚本运行的,提示bad token了,不知道我下面的脚本哪里写错了?
谢谢~

php download.php https://p.ssl.qhimg.com/t01d1f1a2ae31e3c3e4.png 111.png

download.php,获取encodeURI、encodedEntryURI和accessToken;

?\n"
$signingStr = "{$path}?{$query}\n";
$sign = hmac_sha1($signingStr, SecretKey);
$encodedSign = urlsafe_base64_encode($sign);
//$encodeAccessKey = urlsafe_base64_encode(AccessKey);
//$accessToken = "{$encodeAccessKey}:{$encodedSign}";
$accessToken = AccessKey.":{$encodedSign}";
echo "AccessToken:\r\n{$accessToken}\r\n\r\n";
$data['scope'] = BUCKET;
$data['deadline'] = time()+3600;//过期时间

$encoded = urlsafe_base64_encode(json_encode($data));
//$signature=hash_hmac('sha1',$encoded,'KEY',true);
$signature = hmac_sha1($encoded, SecretKey);
$encode_signed = urlsafe_base64_encode($signature);
//$UploadToken=$encode_signed.':'.$encoded; 
$UploadToken = AccessKey.":".$encode_signed.":".$encoded;
echo "AccessToken:\r\n{$UploadToken}\r\n\r\n";

echo "UploadURL:\r\nhttp://iovip.qbox.me/fetch/{$encodeURI}/to/{$encodedEntryURI}\r\n\r\n";

再通过脚本

sh download.sh encodeURI encodedEntryURI accessToken

传输数据,结果。。。提示:

{"error":"bad token"}

download.sh

curl -i \
     -o - \
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -H "Authorization: QBox $3" \
     -X POST \
     "http://iovip.qbox.me/fetch/$1/to/$2"

已经自行处理完成,最终 PHP 部分(有两处错误):

PS:我发现我提问到最后,基本都是靠自己搞定的啊。不知道是不是大牛不屑这些技术含量低的问题还是???
真的和 sg 没法比啊。。。

";
var_dump($signingStr);

$sign = hmac_sha1($signingStr, SecretKey);
$encodedSign = urlsafe_base64_encode($sign);
var_dump($sign);


$accessToken = AccessKey.":{$encodedSign}";

echo "AccessToken:\r\n{$accessToken}\r\n\r\n";

echo "UploadURL:\r\nhttp://iovip.qbox.me/fetch/{$encodeURI}/to/{$encodedEntryURI}\r\n\r\n";

以前写的个:AK,KEYscope替换成你的

$data['scope']='your bucket';
$data['deadline']=time()+3600;//过期时间
$encoded=urlsafe_base64_encode(json_encode($data));
$signature=hash_hmac('sha1',$encoded,'KEY',true);
$encode_signed = urlsafe_base64_encode($signature);
$UploadToken='AK:'.$encode_signed.':'.$encoded;
  
echo $UploadToken;

function urlsafe_base64_encode($data) {
    $data = base64_encode($data);
    $data = str_replace(array('+','/'),array('-','_'),$data);
    return $data;
}

话说为啥不用SDK的?
直接

$token = $auth->uploadToken($bucket);
相关标签: php