Calling Google cloud print /search API from javascript
Asked Answered
L

1

6

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

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.

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.

Leonilaleonine answered 6/5, 2015 at 13:4 Comment(2)
I struggling with the same issue, i tripple check is cors allowed from my side and still getting Origin is therefore not allowed access. from browser Anyone know solution?Disburse
Have you tried this answer?Dehumidifier
K
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

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());
}
Kerenkeresan answered 9/4, 2017 at 16:45 Comment(1)
It appears to me that your answer shows how to have a cloud print button, but the question is about the search API which is a different API.Sinapism

© 2022 - 2024 — McMap. All rights reserved.