I am trying to use gapi
to upload an image to google cloud storage.The current code I have is
<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>
I can see that the code is hitting
the statement in console is: api initialized
but I don't see the gapi.client.request
being called or even printing any error etc.
I am not sure what is wrong here. Please advise
google api
but I can tell one thing that the arguments for thegapi.client.request
arepath
,method
,params
,header
,body
as documented here,please send your input after you have corrected this. – Betsybettacontent type
is not a parameter it should be sent asheader {'Content-type' : 'image/jpeg'}
,please send your inputs after correcting this one – Betsybettagapi.client.request
as apromise
to check whaterror
it throws and provide its input.Helpful link – Betsybettagapi.client.request
intototry
, and see if this statement throws exception. Also, it this site (googleapis.com) reachable at your side? – Diplomatics