【问题标题】:Azure Storage Rest API Authentication Header for Java Android适用于 Java Android 的 Azure Storage Rest API 身份验证标头
【发布时间】:2022-01-30 00:41:50
【问题描述】:

我正在处理一个从 Azure Blob 存储下载图像的 Android 项目,并且收到响应代码 400 Authorization Header not valid。

URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.setConnectTimeout(15000);
                connection.setReadTimeout(15000);
                connection.setRequestMethod("GET");
                Date date = new Date();
                connection.setRequestProperty("Authorization","SAS Signature here?");
                connection.setRequestProperty("x-ms-date", date.toString());
                connection.setRequestProperty("x-ms-version", "2021-02-12");
                connection.connect();
                int response = connection.getResponseCode();
                Log.e("Response Code:",String.valueOf(response));
                Log.e("Response Message:",connection.getResponseMessage());
                InputStream input = connection.getInputStream();
                myBitmap = BitmapFactory.decodeStream(input);
                Log.e("Bitmap","returned");

【问题讨论】:

    标签: java android azure rest authentication


    【解决方案1】:

    如果您使用的是 SAS 令牌,您实际上不必做任何您正在做的事情,因为 SAS 令牌具有所有必要的授权信息。您所要做的就是使用 SAS URL(blob 端点 + SAS 令牌)来创建连接,您应该能够下载 blob。

    类似:

    URL url = new URL(src);//src would be something like https://account.blob.core.windows.net/container/blob.bmp?sastoken
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setConnectTimeout(15000);
    connection.setReadTimeout(15000);
    connection.setRequestMethod("GET");
    connection.connect();
    int response = connection.getResponseCode();
    Log.e("Response Code:",String.valueOf(response));
    Log.e("Response Message:",connection.getResponseMessage());
    InputStream input = connection.getInputStream();
    myBitmap = BitmapFactory.decodeStream(input);
    Log.e("Bitmap","returned");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      相关资源
      最近更新 更多