我正在尝试访问 Google 云打印中的提交 API,但是我遇到了错误“需要用户凭据”。
我能够一路通过身份验证,并且能够检索我的访问 token 。我正在遵循本指南 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 时遇到此错误。
编辑以添加我获得 token 的部分: 我将请求发送至:https://accounts.google.com/o/oauth2/v2/auth 参数: 响应类型:代码, 范围:https://www.googleapis.com/auth/cloud-platform.read-only , 状态:state_parameter_passthrough_value, redirect_uri:脚本 url client_id: 客户端id
Google oauth 返回授权码。我用这种方式将代码换成 token :
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);
}
}
最佳答案
正如我所想,问题在于您如何获取访问 token 。为了获得有效的访问 token 来执行您的云打印业务,您需要包含适当的范围。这意味着您的 params 对象应该像这样:
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'}
这应该会为您提供一个有效的访问 token ,以便与 Google 云打印进行交互。希望对您有所帮助!
关于javascript - 需要用户凭据 : Google Cloud Print Submit API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50650946/