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
*/
Base64_urlSafeEncode ($ data)
{
$find = array('+', '/'); $replace = array('-', '_'); return str_replace($find, $replace, base64_encode($data));
}
/**