Google API call to print / search google from javascript

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

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

XMLHttpRequest cannot load https://www.google.com/cloudprint/search . The requested resource does not have an Access-Control-Allow-Origin header. Origin ' http: // localhost: 8080 ' is therefore not allowed.

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 can use the demo message:

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

from the same web page, and it is successful; I spent quite a bit of time making sure that these two queries look the same in terms of the data and headers presented, but to no avail. I do not want to write this in Java (to avoid server intervention), and would welcome any help.

+6
source share
1 answer

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

Obtaining an OAuth Token and Calling 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()); } 
+1
source

Source: https://habr.com/ru/post/986655/


All Articles