首页 > 解决方案 > 缺少api密钥android Cloudinary上传错误

问题描述

AndroidManifest.xml

 <meta-data
        android:name="CLOUDINARY_URL"
        android:value="${cloudinaryUrl}" />

在我上传文件的活动中,我初始化了 MediaManager

MediaManager.init(this);

我已经使用它的uri上传了文件:

val requestId = MediaManager.get().upload(uri).callback(object : UploadCallback {
    override fun onStart(requestId: String) {
        // your code here
    }

    override fun onProgress(requestId: String, bytes: Long, totalBytes: Long) {
        // example code starts here
        val progress = bytes.toDouble() / totalBytes
        // post progress to app UI (e.g. progress bar, notification)
        // example code ends here
    }

    override fun onSuccess(requestId: String, resultData: Map<*, *>) {
        // your code here
    }

    override fun onError(requestId: String, error: ErrorInfo) {
        // your code here
    }

    override fun onReschedule(requestId: String, error: ErrorInfo) {
        // your code here
    }
}).dispatch()

问题是我得到了一个错误'Missing api key'。我需要一个 api 密钥来进行未签名的上传吗?

标签: androidcloudinary

解决方案


通过在文档中查看此处。在示例中,您需要通过SignatureProvider

示例代码:

Map config = new HashMap();
config.put("cloud_name", "myCloudName");
MediaManager.init(this, new SignatureProvider(){
  @Override
  public Signature provideSignature(Map options) {
    // replace the following with a function that calls your backend signature generation endpoint
    SignResult res = signUpload(options);  // example name of a function that implements a synchronous HTTPS call
    return new Signature(res.getSignature(), res.getApiKey(), res.getTimestamp());
  }
  @Override
  public String getName() {
    return "SampleSignatureProvider"; // for logging purposes
  }
}, config);

要启用签名上传,您需要使用您的类实例的名称 (init(Context, SignatureProvider, Map)) 更新对 MediaManager 的 init 方法的调用。只要上传必须签名,您的课程就会被实施。

请参阅SignatureProvider接口

编辑

对于未签名的上传,您没有提到.unsigned("sample_preset"). MediaManager.get().upload(uri)有关更多信息,请参见此处


推荐阅读