javascriptgoogle-apinetsuitegoogle-oauthgoogle-cloud-print

User credentials required: Google Cloud Print Submit API


I am trying to access the Submit API that is apart of the Google Cloud Print however I am running into the error "User credentials required".

I am able to get all the way through authentication and am able to retrieve my access token. I was following this guide https://developers.google.com/cloud-print/docs/appDevGuide

I do not know where it is going wrong. Does anyone know where the credentials are supposed to be inputted?

Here is my code for this portion:

function submitPrintJob(token){
        try{

            var params = {'printerid':'PRINTER_ID','title':'Test Print Job','ticket':{"version": "1.0", "print": {}},'contentType':'application/pdf'};
            var response = https.post({
                url: 'https://www.google.com/cloudprint/submit',
                body: params,
                headers: {'Authorization':'Bearer ' + token}
            });

            log.debug('submitPrintJob','Response - ' + response.body);
        }catch(e){
            log.error('submitPrintJob','Error - ' + e.message);
        }
    }

This code is being in done in Netsuite which is where the https.post API is coming in. I also am aware that I am not sending a document through but I at least need to get past this step. I am getting this error using Postman as well.

Editing to add the portion where I get the token: I send a request to: https://accounts.google.com/o/oauth2/v2/auth Parameters: response_type: code, scope: https://www.googleapis.com/auth/cloud-platform.read-only, state: state_parameter_passthrough_value, redirect_uri: scriplet url client_id: client id

Google oauth returns a authorization code. I exchange the code for a token this way:

        function getToken(code){
            try{

                var params = {'code':code,'client_id':CLIENT_ID,'client_secret':CLIENT_SECRET,'redirect_uri':REDIRECT_URI,'grant_type':'authorization_code'}
                var response = https.post({
                    url: 'https://www.googleapis.com/oauth2/v4/token',
                    body: params,
                    headers: {'Content-Type':'application/x-www-form-urlencoded'}
                });

                var token = response.body.access_token;

                submitPrintJob(token);

            }catch(e){
                log.error('getAuthCode','Error - ' + e.message);
            }
        }

Solution

  • As I thought, the problem is how you are getting the access token. In order to get a valid access token to do your cloud print business, you need to include the proper scope. That means that your params object should like like this:

    var params = {'code':code,'client_id':CLIENT_ID,'client_secret':CLIENT_SECRET,'redirect_uri':REDIRECT_URI, 'scope': 'https://www.googleapis.com/auth/cloudprint', 'grant_type':'authorization_code'}
    

    This should give you a valid access token to interact with Google Cloud Print. I hope it helps!