代码:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const OAuthClient = require('intuit-oauth');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const urlencodedParser = bodyParser.urlencoded({ extended: false });
oauthClient = new OAuthClient({
clientId: 'ABmWLisPCBmxAXMM24r7HK6wK8lWpWOlybWSinDUAkW0l3FZsj',
clientSecret: '4SRQWIXv547bUKuyDk7xxw1YIRTLddXJA11f2zhc',
environment: 'sandbox',
redirectUri: 'http://localhost:8000/callback', //We have to add callback URL to quickbooks dashboard
});
app.get('/authUri', urlencodedParser, function (req, res) {
const authUri = oauthClient.authorizeUri({
scope: [OAuthClient.scopes.Accounting],
state: 'intuit-test',
});
res.send(authUri);
// Here we start the authentication process
});
app.get('/callback', function (req, res) {
oauthClient
.createToken(req.url)
.then(function (authResponse) {
oauth2_token_json = JSON.stringify(authResponse.getJson(), null, 2);
console.log("Token:", oauth2_token_json)
res.send(oauth2_token_json);
})
.catch(function (e) {
console.log("ERR:",e);
res.send(e);
});
//Here we recieve the token succesfully or get the error.
});
app.listen(8000,()=>{
console.log('i am running');
});
const body = {
"Line": [
{
"DetailType": "SalesItemLineDetail",
"Amount": 100.0,
"SalesItemLineDetail": {
"ItemRef": {
"name": "Services",
"value": "1"
}
}
}
],
"CustomerRef": {
"value": "1"
}
};
oauthClient
.makeApiCall({
url: 'https://sandbox-quickbooks.api.intuit.com/v3/company/4620816365092507370/invoice?minorversion=55',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
.then(function (response) {
console.log('The API response is : ' + response);
})
.catch(function (e) {
console.log('The error is ' + JSON.stringify(e));
});
错误:
错误是:
{
"authResponse":{
"token":{
"realmId":"",
"token_type":"",
"access_token":"",
"refresh_token":"",
"expires_in":0,
"x_refresh_token_expires_in":0,
"id_token":"",
"latency":60000,
"createdAt":1603006471243
},
"response":{
"url":"https://sandbox-quickbooks.api.intuit.com/v3/company/4620816365092507370/invoice?minorversion=55",
"headers":{
"date":"Sun, 18 Oct 2020 07:34:32 GMT",
"content-type":"application/json",
"content-length":"401",
"connection":"close",
"server":"nginx",
"intuit_tid":"1-5f8bf008-0186e88168aa182213feefcf",
"www-authenticate":"Bearer realm=\"Intuit\", error=\"invalid_request\""
},
"body":"{\"warnings\":null,\"intuitObject\":null,\"fault\":{\"error\":[{\"message\":\"message=BadRequest; errorCode=003202; statusCode=400\",\"detail\":\"Missing required parameter: access_token\",\"code\":\"3202\",\"element\":null}],\"type\":\"VALIDATION\"},\"report\":null,\"queryResponse\":null,\"batchItemResponse\":[],\"attachableResponse\":[],\"syncErrorResponse\":null,\"requestId\":null,\"time\":1603006472539,\"status\":null,\"cdcresponse\":[]}",
"status":400,
"statusText":"Bad Request"
},
"body":"{\"warnings\":null,\"intuitObject\":null,\"fault\":{\"error\":[{\"message\":\"message=BadRequest; errorCode=003202; statusCode=400\",\"detail\":\"Missing required parameter: access_token\",\"code\":\"3202\",\"element\":null}],\"type\":\"VALIDATION\"},\"report\":null,\"queryResponse\":null,\"batchItemResponse\":[],\"attachableResponse\":[],\"syncErrorResponse\":null,\"requestId\":null,\"time\":1603006472539,\"status\":null,\"cdcresponse\":[]}",
"json":{
"warnings":null,
"intuitObject":null,
"fault":{
"error":[
{
"message":"message=BadRequest; errorCode=003202; statusCode=400",
"detail":"Missing required parameter: access_token",
"code":"3202",
"element":null
}
],
"type":"VALIDATION"
},
"report":null,
"queryResponse":null,
"batchItemResponse":[
],
"attachableResponse":[
],
"syncErrorResponse":null,
"requestId":null,
"time":1603006472539,
"status":null,
"cdcresponse":[
]
},
"intuit_tid":"1-5f8bf008-0186e88168aa182213feefcf"
},
"originalMessage":"Response has an Error",
"error":"Bad Request",
"error_description":"Bad Request",
"intuit_tid":"1-5f8bf008-0186e88168aa182213feefcf"
}