Seven Cattle private space resource access will occasionally appear 401 authorization errors, the same resource can be accessed sometimes, and can not access

Source: Internet
Author: User
The specific situation is:
One page I need to request three certificate picture resources from seven kn private space, in the local database I stored the names of these certificates, in the background code I based on the file name to generate these certificates can access the private link address, the front-end with the ANGULARJS framework, the background code is as follows:

  $coach _cert = new Coach_cert ();                $results = $coach _cert->listcerts ($this->coach_id);        Seven bull space key $accessKey = ';        $secretKey = ";        Private space name $bucket = ';        Timestamp generation $now = time ();        $date = $now + 60; Download voucher generation foreach ($results as & $result) {$download _url = "http://". $bucket. ". 7xnhmh.com2.z0.glb.qiniucdn.com/". $result [' filename ']. "? e=".            $date;            $sign = Hash_hmac ("SHA1", $download _url, $secretKey, true);            $encodedSign = Base64_encode ($sign); $token = $accessKey. ":" .            $encodedSign; $realdownloadurl = $download _url. "&token=".            $token;        $result [' filename '] = $realdownloadurl; } return [' certs ' = ' $results];  

Now the problem is: Click to view three certificates, sometimes three certificates can all be loaded out, sometimes can only load parts, can not load in the console report 401 authorization failure Error: Error: "Download token auth failed"

Reply content:

The specific situation is:
One page I need to request three certificate picture resources from seven kn private space, in the local database I stored the names of these certificates, in the background code I based on the file name to generate these certificates can access the private link address, the front-end with the ANGULARJS framework, the background code is as follows:

  $coach _cert = new Coach_cert ();                $results = $coach _cert->listcerts ($this->coach_id);        Seven bull space key $accessKey = ';        $secretKey = ";        Private space name $bucket = ';        Timestamp generation $now = time ();        $date = $now + 60; Download voucher generation foreach ($results as & $result) {$download _url = "http://". $bucket. ". 7xnhmh.com2.z0.glb.qiniucdn.com/". $result [' filename ']. "? e=".            $date;            $sign = Hash_hmac ("SHA1", $download _url, $secretKey, true);            $encodedSign = Base64_encode ($sign); $token = $accessKey. ":" .            $encodedSign; $realdownloadurl = $download _url. "&token=".            $token;        $result [' filename '] = $realdownloadurl; } return [' certs ' = ' $results];  

Now the problem is: Click to view three certificates, sometimes three certificates can all be loaded out, sometimes can only load parts, can not load in the console report 401 authorization failure Error: Error: "Download token auth failed"

Your signature has a problem, we are using safebase64 code, you can refer to our source code
https://github.com/qiniu/php-sdk/blob/a864511adb2e50b2b0f080440e6f11d6989905ec/src/Qiniu/functions.php

URL-Safe Base64 encoding is suitable for scenarios in which BASE64 encoding results are delivered in a URL manner. The basic procedure for this encoding is to encode the content in Base64 format as a string, then examine the result string, replace the Plus + + in the string with an underscore-and slash/change to underscore _, while the tail remains filled with equals =.

/**

    • Urlsafe the base64 encoding of the supplied data.
      *

    • @param string $data the data to be encoded, typically a string
      *

    • @return string-encoded strings

    • @link http://developer.qiniu.com/docs/v6/api/overview/appendix.html#urlsafe-base64
      */

      1. Base64_urlsafeencode ($data)
        {

           $find = array('+', '/');   $replace = array('-', '_');   return str_replace($find, $replace, base64_encode($data));

        }
        /**

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.