需要用户凭据:Google 云打印提交 API

User credentials required: Google Cloud Print Submit API

我正在尝试访问属于 Google 云打印的提交 API,但是我 运行 进入了错误 "User credentials required"。

我能够完全通过身份验证并能够检索我的访问令牌。我正在按照本指南进行操作 https://developers.google.com/cloud-print/docs/appDevGuide

不知道哪里出错了。有谁知道应该在哪里输入凭据?

这是我对这部分的代码:

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

这段代码是在 Netsuite 中完成的,这是 https.post API 进来的地方。我也知道我没有通过发送文档,但我至少需要得到过了这一步。我也使用 Postman 遇到此错误。

编辑以添加我获得令牌的部分: 我发送请求至:https://accounts.google.com/o/oauth2/v2/auth 参数: response_type: 代码, 范围:https://www.googleapis.com/auth/cloud-platform.read-only, 状态:state_parameter_passthrough_value, redirect_uri:脚本 url client_id: 客户编号

Google oauth returns 一个授权码。我用这种方式将代码换成令牌:

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

正如我所想,问题在于您如何获取访问令牌。为了获得有效的访问令牌来执行您的云打印业务,您需要包含适当的范围。这意味着您的参数对象应该像这样:

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

这应该会给你一个有效的访问令牌来与 Google 云打印交互。希望对您有所帮助!