我正在尝试使用gapi
将图像上传到Google云存储。目前我的代码是这样的:
<script src="https://apis.google.com/js/api.js"></script>
<script type="text/javascript">
var imgData = null;
function getImage() {
navigator.camera.getPicture(onSuccess, onFailure, {
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
function onSuccess(imageURI) {
imgData = encodeImageUri(imageURI);
var contentLen = imgData.length;
gapi.load('client', start);
}
function onFailure(message) {
alert("Get image failed: " + message);
}
}
function start() {
// 2. Initialize the JavaScript client library.
console.log('firing google storage api');
gapi.client.init({
'apiKey': 'XXX-XX'
}).then(function() {
// 3. Initialize and make the API request.
console.log('api initialized');
var request = gapi.client.request({
'path': 'https://www.googleapis.com/upload/storage/v1/b/visionapibucket/o?uploadType=media&name=myObject',
'method': 'POST',
'headers': {
'Content-Type': 'image/jpeg'
},
'body': imgData
});
try {
//Execute the insert object request
console.log('executing call');
request.execute(function(resp) {
alert(resp);
});
} catch (e) {
console.log('An error has occurred: ' + e.message);
}
}).then(function(response) {
console.log(response.result);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
};
</script>
我可以看到代码在控制台中触发了语句:api initialized
但是我没有看到gapi.client.request
被调用或打印任何错误信息等。
我不确定出了什么问题,请指导。
google api
不是很了解,但我可以告诉你一件事情,gapi.client.request
的 参数 是path
、method
、params
、header
和body
,如此文档所述 here,请在您纠正后发送您的输入。 - Pritish Vaidyagapi.client.request
放进try
里面,看看这个语句是否会抛出异常吗?此外,你那边能否访问到这个网站(https://www.googleapis.com/)? - zhm