七牛云 获取 Bucket 空间域名失败

按照官网提供的接口,最后是bad token ??

imageimageimage

阅读 2.4k
2 个回答

看起来是 token 签算有问题,所以报错了 bad token,如下提供一个 PHP 的 demo 供楼主参考

<?php
require '../vendor/autoload.php';
use \Qiniu\Auth;
use Qiniu\Http\Client;
 
$accessKey = 'xxxx';
$secretKey = 'xxxx';
$auth = new Auth($accessKey, $secretKey);
 
$url = "http://api.qiniu.com/v6/domain/list?tbl=test";
$method = "GET";
$host = "api.qiniu.com ";
$contentType = "application/x-www-form-urlencoded";
 
//$headers = $auth->authorization($url, $method);
$headers = $auth->authorizationV2($url, $method,null,$contentType);
 
$headers['Host'] = $host;
$headers['Content-Type'] = $contentType;
 
$response = Client::get($url, $headers);
 
if ($response->ok()) {
    $r=$response->json();
    var_dump($r);
}else{
    var_dump($response);
}

qiniu/nodejs-sdk

const qiniu = require('qiniu');
const mac = new qiniu.auth.digest.Mac(ak, sk);
const accessToken = qiniu.util.generateAccessTokenV2(
   mac,
  'http://api.qiniu.com/v6/domain/list?tbl=空间名称',
  'GET',
  'application/x-www-form-urlencoded');
console.log(accessToken);

Nodejs

const crypto = require('crypto');
const ak = 'xxxx';
const sk = 'xxx';
const data = 'GET /v6/domain/list?tbl=空间名称\nHost: api.qiniu.com\nContent-Type: application/x-www-form-urlencoded\n\n';
const hmac = crypto.createHmac('sha1', sk);
hmac.update(data);

const key = hmac.digest('base64');
const accessToken = ak
 + ':'
 + key
 .replace(/+/g, '-')  // /\+/g
 .replace(///g, '_'); // /\//g
console.log('Qiniu ' + accessToken);
logo
七牛云问答
子站问答
访问
宣传栏