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

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

imageimageimage

阅读 122
评论
    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);
    }
      • 90

      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);
        撰写回答

        登录后参与交流、获取后续更新提醒

        相似问题
        七牛云问答
        合作问答

        七牛云 联合 SegmentFault 共同推出技术问答专区,为开发者们提供社区支持和帮助。在这里,你将直接获得...

        推荐文章