HttpURLConnection PUT到Google云端存储,提供错误403 [英] HttpURLConnection PUT to Google Cloud Storage giving error 403

查看:291
本文介绍了HttpURLConnection PUT到Google云端存储,提供错误403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用XML API将文件上传到Google云端存储。我为每次上传生成了正确的GoogleAccessId,到期日期和签名。奇怪的是,我可以使用Postman(Chrome应用程序)PUT文件,所以我确定网址没问题。我只是不能使用我的Android Java程序(它返回给我403错误)。执行上传的源代码在此处(基于此: https://cloud.google。 com / storage / docs / access-control#Signing-Strings ):

I tried to upload a file to Google Cloud Storage using XML API. I have the right GoogleAccessId, expiry date and signature generated for each upload. The strange thing is that I can PUT file using Postman (application for Chrome), so I'm sure that the URL is ok. I just cannot PUT it using my Android Java program (it returns to me 403 error). The source code performing upload is here (it base on this one: https://cloud.google.com/storage/docs/access-control#Signing-Strings):

    URL url;
    HttpURLConnection connection;

    try {
        url = new URL("http://google-testbucket.storage.googleapis.com/testdata.txt?GoogleAccessId=1234567890123@developer.gserviceaccount.com&Expires=1331155464&Signature=BClz9e4UA2MRRDX62TPd8sNpUCxVsqUDG3YGPWvPcwN%2BmWBPqwgUYcOSszCPlgWREeF7oPGowkeKk7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9sFpqXsQI8IQi1493mw%3D");
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");

        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("Test");
        out.close();

        Log.i("TAG", "PUT Response code: " + connection.getResponseCode());

    } catch (MalformedURLException e) {
        e.printStackTrace();
        Log.e("TAG", "MalformedURLException");
    } catch (ProtocolException e) {
        e.printStackTrace();
        Log.e("TAG", "ProtocolException");
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("TAG", "IOException");
    }

PUT对象的文档:https://cloud.google.com/storage/docs/xml-api/put-object-upload

Documentation for PUT Object: https://cloud.google.com/storage/docs/xml-api/put-object-upload

任何人都可以调查这个问题并告诉我这个问题可能出现什么问题吗?

Can anybody look into this problem and give me hints what might went wrong with this one?

推荐答案

我刚刚发现 HttpURLConnection 添加 Content-Type 标头,其值为 application / x-www-form-urlencoded 本身。我在我的android模拟器上使用HTTP嗅探器完成了它。

I just figured out that HttpURLConnection adds Content-Type header with value application/x-www-form-urlencoded by itself. I've done it using HTTP sniffer on my android emulator.

这个自动添加的标头导致签名不匹配。在我更改服务器端的代码以允许使用 Content-Type:application / x-www-form-urlencoded 的请求后,它会生成正确的签名,并且工作正常。

This auto-added header caused signature mismatch. After I changed the code on the server-side to allow requests with Content-Type: application/x-www-form-urlencoded it generates the right signature and it works fine.

感谢@ morpheus05的承诺。

Thank you @morpheus05 for your commitment.

这篇关于HttpURLConnection PUT到Google云端存储,提供错误403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆