我尝试使用XML API将文件上传到Google云端存储.我为每次上传生成了正确的GoogleAccessId,到期日期和签名.奇怪的是我可以使用Postman(Chrome应用程序)PUT文件,所以我确定网址没问题.我只是不能使用我的Android Java程序(它返回给我403错误).执行上传的源代码在此处(基于以下内容: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
任何人都可以调查这个问题,并告诉我这个问题可能出现什么问题?
我只是想通过自己HttpURLConnection
添加Content-Type
标头值application/x-www-form-urlencoded
.我在我的android模拟器上使用HTTP嗅探器完成了它.
此自动添加的标头导致签名不匹配.在我更改服务器端的代码以允许与Content-Type: application/x-www-form-urlencoded
它的请求生成正确的签名后,它工作正常.
谢谢@ morpheus05的承诺.