Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript调用Google云打印/搜索API_Javascript_Cors_Google Cloud Print - Fatal编程技术网

从javascript调用Google云打印/搜索API

从javascript调用Google云打印/搜索API,javascript,cors,google-cloud-print,Javascript,Cors,Google Cloud Print,是否有人成功地使用了JavaScript中的Google Cloud Print(特别是/search)API 我已经尝试了很多方法,但是不断地出现以下错误 无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“” 代码段: var search = new XMLHttpRequest(); search.open('POST', 'https://www.google.com/cloudp

是否有人成功地使用了JavaScript中的Google Cloud Print(特别是/search)API

我已经尝试了很多方法,但是不断地出现以下错误

无法加载XMLHttpRequest。请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源“”

代码段:

    var search = new XMLHttpRequest();

    search.open('POST', 'https://www.google.com/cloudprint/search', true);
    search.withCredentials = true;          
    search.setRequestHeader("X-Cloud-Print", "Google-JS");

    search.onreadystatechange = function(response){
            console.log(response);
    };

    search.send();
我可以使用演示表单帖子:

    <form action="https://www.google.com/cloudprint/search" method="post" enctype="multipart/form-data" id="submitForm">
      <input type="submit" value="Search"/>
    </form>     

从完全相同的网页,它是成功的;我花了相当长的时间来确保这两个请求在提交的数据和标题方面看起来是相同的,但没有用。我不愿意用Java写这篇文章(因为我试图避免服务器后端参与),我欢迎任何帮助。

Auth.html:

<a href='<?!= getAuthURL(); ?>' target='_blank'>
<button>Authorize!</button>
</a>
获取oAuth令牌并调用Google Cloud Print

function getAuthResponse(q) {
  var options = {
    method: "post",
    muteHttpExceptions: true,
    payload: {
      code: q.parameter.code,
      client_id : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", // your id 
      client_secret : "xxxxxxxxxxxxxxxxxxxxxxxx", // your secret
      redirect_uri: "https://script.google.com/macros/d/MDYeOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/usercallback", // your uri
      grant_type: "authorization_code"
    }
  }
  var response = JSON.parse(UrlFetchApp.fetch("https://accounts.google.com/o/oauth2/token", options));
  var auth_string = response.token_type+" "+response.access_token;
  options.method = "get";
  options.payload = null;
  options.headers = {Authorization: auth_string};
  response = UrlFetchApp.fetch("https://www.google.com/cloudprint/search",options);
  return ContentService.createTextOutput(response.getContentText());
}

我在同一个问题上苦苦挣扎,我的三重检查被允许从我这边获得cors,并且仍然获得
Origin因此不允许访问。从浏览器中
有人知道解决方案吗?您尝试过吗?在我看来,您的答案显示了如何使用云打印按钮,但问题是关于搜索API,它是一种不同的API。
function getAuthResponse(q) {
  var options = {
    method: "post",
    muteHttpExceptions: true,
    payload: {
      code: q.parameter.code,
      client_id : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", // your id 
      client_secret : "xxxxxxxxxxxxxxxxxxxxxxxx", // your secret
      redirect_uri: "https://script.google.com/macros/d/MDYeOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/usercallback", // your uri
      grant_type: "authorization_code"
    }
  }
  var response = JSON.parse(UrlFetchApp.fetch("https://accounts.google.com/o/oauth2/token", options));
  var auth_string = response.token_type+" "+response.access_token;
  options.method = "get";
  options.payload = null;
  options.headers = {Authorization: auth_string};
  response = UrlFetchApp.fetch("https://www.google.com/cloudprint/search",options);
  return ContentService.createTextOutput(response.getContentText());
}