When a qiniu private space resource is accessed, the 401 authorization error may occur from time to time. The same resource is sometimes accessible and sometimes inaccessible.

Source: Internet
Author: User
The specific situation is: I need to request three certificate image resources from qiniu private space on a page. In the local database, I store the names of these certificates, in the background code, I generate the private link addresses that these certificates can access in sequence based on the file name. The AngularJS framework used at the front end is used as the background code... the specific situation is:
On a page, I need to request three certificate image resources from qiniu's private space. In the local database, I store the names of these certificates, in the background code, I generate private links that can be accessed by these certificates based on the file name. The AngularJS framework is used at the front end. The background code is as follows:

$ Coach_cert = new coach_cert (); $ results = $ coach_cert-> listCerts ($ this-> coach_id); // qiniu Space key $ accessKey = ''; $ secretKey = ''; // private space name $ bucket =''; // time stamp generated $ now = time (); $ date = $ now + 60; // generate 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 all three certificates can be loaded. Sometimes only some of them can be loaded. If not, the error 401 failed authorization is reported on the console: error: "download token auth failed"

Reply content:

The specific situation is:
On a page, I need to request three certificate image resources from qiniu's private space. In the local database, I store the names of these certificates, in the background code, I generate private links that can be accessed by these certificates based on the file name. The AngularJS framework is used at the front end. The background code is as follows:

$ Coach_cert = new coach_cert (); $ results = $ coach_cert-> listCerts ($ this-> coach_id); // qiniu Space key $ accessKey = ''; $ secretKey = ''; // private space name $ bucket =''; // time stamp generated $ now = time (); $ date = $ now + 60; // generate 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 all three certificates can be loaded. Sometimes only some of them can be loaded. If not, the error 401 failed authorization is reported on the console: error: "download token auth failed"

Your signature is incorrect. We use safebase64 encoding. You can refer to our source code.
Https://github.com/qiniu/php-sdk/blob/a864511adb2e50b2b0f080440e6f11d6989905ec/src/Qiniu/functions.php

URL-safe Base64 encoding is applicable to the scenario where Base64 encoding results are transmitted in URL mode. The basic process of this encoding method is to first encode the content in Base64 format as a string, then check the result string, replace the plus sign + in the string with a hyphen -, in addition, the slash/is replaced with underscore _, and the tail is always filled with equal signs =.

/**

    • Encode the provided data with URL safe base64.
      *

    • @ Param string $ data the data to be encoded, which is generally a string
      *

    • @ Return string encoded string

    • @ 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.