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

时间:2021-08-18 17:37:09

Has anyone had any success using the Google Cloud Print (specifically the /search) API from JavaScript?

有没有人使用JavaScript的Google Cloud Print(特别是/ search)API取得了成功?

I have tried any number of ways but keep getting the following error.

我尝试了很多方法,但不断收到以下错误。

XMLHttpRequest cannot load https://www.google.com/cloudprint/search. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

XMLHttpRequest无法加载https://www.google.com/cloudprint/search。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许来源“http:// localhost:8080”访问。

Code snippet:

    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();

I am able to use the demo form post:

我可以使用演示表格帖子:

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

from exactly the same webpage and it is successful; i have spent quite some time making sure that the two requests look identical in terms of submitted data and headers but to no avail. I am reluctant to have to write this in Java (as trying to avoid server backend involvement) and would welcome any help.

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

1 个解决方案

#1


Auth.html:

<a href='<?!= getAuthURL(); ?>' target='_blank'>
<button>Authorize!</button>
</a>

Authorize.

  function test() {
  var html = HtmlService.createTemplateFromFile("Auth").evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE).setTitle("Test");
  SpreadsheetApp.getUi().showModalDialog(html, "Test");
}
function getAuthURL() {
  var options= {
    client_id : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", // your id
    scope : "https://www.googleapis.com/auth/cloudprint",
    redirect_uri : "https://script.google.com/macros/d/MDYeOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/usercallback", // your uri
    state : ScriptApp.newStateToken().withMethod("getAuthResponse").createToken()
  };
  var url = "https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline";
  for(var i in options) 
    url += "&"+i+"="+encodeURIComponent(options[i]);
  return url;
}

Getting oAuth token and call Google Cloud Print

获取oAuth令牌并致电Google云打印

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());
}

#1


Auth.html:

<a href='<?!= getAuthURL(); ?>' target='_blank'>
<button>Authorize!</button>
</a>

Authorize.

  function test() {
  var html = HtmlService.createTemplateFromFile("Auth").evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE).setTitle("Test");
  SpreadsheetApp.getUi().showModalDialog(html, "Test");
}
function getAuthURL() {
  var options= {
    client_id : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com", // your id
    scope : "https://www.googleapis.com/auth/cloudprint",
    redirect_uri : "https://script.google.com/macros/d/MDYeOxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/usercallback", // your uri
    state : ScriptApp.newStateToken().withMethod("getAuthResponse").createToken()
  };
  var url = "https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline";
  for(var i in options) 
    url += "&"+i+"="+encodeURIComponent(options[i]);
  return url;
}

Getting oAuth token and call Google Cloud Print

获取oAuth令牌并致电Google云打印

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());
}