0

我正在尝试使用StreetView Publish API从 JavaScript 上传照片,似乎我尝试的一切都失败了......目前我有 uploadUrl,我需要使用实际图像数据发出发布请求

这就是我最终做的

var input = document.querySelector('input[type="file"]').files;
var reader = new FileReader();
reader.onload = function(){
      var dataURL = reader.result;
      var xhr = new XMLHttpRequest();
      xhr.open("POST", window.uploadUrl, true);
      xhr.setRequestHeader("Authorization", gapi.client.getToken().token_type + ' ' + gapi.client.getToken().access_token);
      xhr.setRequestHeader("Content-Type", "image/jpeg");
      xhr.setRequestHeader("X-Goog-Upload-Protocol", "raw");
      xhr.setRequestHeader("X-Goog-Upload-Content-Length", dataURL.length );
      xhr.onreadystatechange = function() {//Call a function when the state changes.
        if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
            console.log(xhr);
        }
      }
      xhr.send(dataURL);
    };
reader.readAsDataURL(input[0]);

我得到的答案如下:加载失败

https://streetviewpublish.googleapis.com/media/user/.../photo/ ...:预检响应无效(重定向)

任何人都可以提出任何可能的解决方案吗?

谢谢

从我看到的更新 ...当我尝试上传图像时,会生成 2 个请求...都是 OPTIONS 和 302 状态,它们都没有我要发送的标头...主要是访问令牌

var xhr = new XMLHttpRequest();
xhr.open("POST", window.uploadUrl + "?key=...", true);
xhr.setRequestHeader("Authorization", gapi.client.getToken().token_type + ' ' + gapi.client.getToken().access_token);
xhr.setRequestHeader("Authorization", gapi.client.getToken().access_token);
xhr.setRequestHeader("Content-Type", 'image/jpeg');
xhr.setRequestHeader("X-Goog-Upload-Protocol", "raw");
xhr.setRequestHeader("X-Goog-Upload-Content-Length", dataURL.length );
xhr.onreadystatechange = function() {
   if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
      console.log(xhr);
   }
}
xhr.send(dataURL);
4

0 回答 0