android端生成token报错bad token
博亿娱乐平台下载
博亿娱乐平台下载
当前位置 : 博亿娱乐平台下载 > 博亿国际娱乐首页

android端生成token报错bad token

public class UploadToken  {
    public static  String getUploadTokenObject object{
        Gson gson=new Gson;
        String jsonObject=gson.toJsonobject;
        String encodedPutPolicy = UrlSafeBase64.encodeToStringjsonObject;
        String encodedSign="";
        try {
            encodedSign =hmacSha1encodedPutPolicy;
        }catch Exception e{

        }
        String uploadToken = QiNiuConstant.ACCESSKEY + ":" + encodedSign + ":" + encodedPutPolicy;
        return uploadToken;
    }
    public  static   String hmacSha1final String base
            throws NoSuchAlgorithmException, InvalidKeyException {
        String type = "HmacSHA1";
        SecretKeySpec secret = new SecretKeySpecQiNiuConstant.SECRETKEY.getBytes, type;
        Mac mac = Mac.getInstancetype;
        mac.initsecret;
        byte[] digest = mac.doFinalbase.getBytes;
        return Base64.encodeToStringdigest, Base64.URL_SAFE;
    }

其中Object的为:

public class ImagePutPolicy implements Serializable {
    private String scope;
    private long deadline;
    private ReturnBody returnBody;
    public ImagePutPolicyString fileName {
        scope = QiNiuConstant.SAVESPACE+":"+fileName;
    }

    public long getDeadline {
        return deadline;
    }

    public void setDeadlinelong deadline {
        this.deadline = System.currentTimeMillis/1000+deadline;
    }

    public ReturnBody getReturnBody {
        return returnBody;
    }

    public void setReturnBodyReturnBody returnBody {
        this.returnBody = returnBody;
    }
}

上传代码为

  private void simpleUpload {
        ImagePutPolicy putPolicy=new ImagePutPolicykey;
        putPolicy.setDeadline3600;
        token=UploadToken.getUploadTokenputPolicy;
        byte[] data=new byte[]{ 0, 1, 2, 3};
        
        uploadManager.putdata, key, token,new UpCompletionHandler {
            @Override
            public void completeString key, ResponseInfo info, JSONObject response {
                //  res 包含hash、key等信息,具体字段取决于上传策略的设置。
                Log.i"qiniu", key + ",\r\n " + info + ",\r\n " + response;
            }
        }, new UploadOptionsnull, "test-type", true, null, null;

    }

token不是后台生成过来的么

-----------------------------------7-29编辑-------------------------------------------------

这应该是七牛上传,所以需要安卓端生成token。如果是七牛上传,代码如下:

public String getToken {
        try {
            JSONObject _json = new JSONObject;
            long _dataline = System.currentTimeMillis / 1000 + 3600;
            _json.put"deadline", _dataline;// 有效时间为一个小时
            _json.put"scope", "images";
            String _encodedPutPolicy = UrlSafeBase64.encodeToString_json
                    .toString.getBytes;
            byte[] _sign = HmacSHA1Encrypt_encodedPutPolicy, SecretKey;
            String _encodedSign = UrlSafeBase64.encodeToString_sign;
            String _uploadToken = AccessKey + : + _encodedSign + :
                    + _encodedPutPolicy;
            return _uploadToken;
        } catch Exception e {
            e.printStackTrace;
            return "";
        }
    }
    
    
 /**
     * 
     * 使用 HMAC-SHA1 签名方法对对encryptText进行签名
     * 
     * @param encryptText
     *            被签名的字符串
     * @param encryptKey
     *            密钥
     * @return
     * @throws Exception
     */
    public static byte[] HmacSHA1EncryptString encryptText, String encryptKey
            throws Exception {
        byte[] data = encryptKey.getBytesENCODING;
        // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
        SecretKey secretKey = new SecretKeySpecdata, MAC_NAME;
        // 生成一个指定 Mac 算法 的 Mac 对象
        Mac mac = Mac.getInstanceMAC_NAME;
        // 用给定密钥初始化 Mac 对象
        mac.initsecretKey;
        byte[] text = encryptText.getBytesENCODING;
        // 完成 Mac 操作
        return mac.doFinaltext;
    }

希望对你有帮助,以上

http://www.yl1001.com/group_article/4101468381601608.htm?classic_id=6551468816785644#comment_6551468816785644

我问了一下朋友,他说一般常规做法是后端生成token。有什么问题可以在这个帖子回复问一下他

1、第一次听说token是移动端生成的
2、不禁想问:你们的服务端在忙什么

广告位

博亿娱乐平台下载