如何解决Square Node SDK API调用返回“未经授权”错误
我目前正在尝试通过它们的node.js square-connect包使用三个Square API。但是,由于授权错误,我似乎无法访问它们中的任何一个。我用来授权的代码(使用节点sdk)可以在下面看到。 注意:我已将“我的令牌”和“我的位置”替换为正确的值。
var oauth2 = square.ApiClient.instance.authentications['oauth2'];
oauth2.accessToken = 'My token'; //Production accessToken
var customerApi = new square.CustomersApi();
var catalogApi = new square.CatalogApi();
var ordersApi = new square.OrdersApi();
var locationId = "My location"; //production location
然后我使用Square SDK这样进行API调用(这只是一个示例)
async function searchCustomers(data) {
var body = {
query: {
filter: {
reference_id: {
exact: data.member.toString() //We are filtering the customers by an exact match with the reference id (member id)
}
}
}
}
return await customerApi.searchCustomers(body).then(function (data) {
// Note: We only return the first customer
if (data.customers.length <= 0) {
console.log("No customers found to have a membership id of: " + data.member);
return null;
}
console.log('search Customers API called successfully. Returned data: ' + data);
return data.customers[0]; // This should be a customer object
}).catch(e => {
console.log("Could not search customers.",e)
return null;
})
错误消息:
Could not search customers. { Error: Unauthorized
0|maaNode | at Request.callback (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/index.js:706:15)
0|maaNode | at parser (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/index.js:916:18)
0|maaNode | at IncomingMessage.res.on (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/parsers/json.js:19:7)
0|maaNode | at emitNone (events.js:111:20)
0|maaNode | at IncomingMessage.emit (events.js:208:7)
0|maaNode | at endReadableNT (_stream_readable.js:1064:12)
0|maaNode | at _combinedTickCallback (internal/process/next_tick.js:138:11)
0|maaNode | at process._tickCallback (internal/process/next_tick.js:180:9)
0|maaNode | status: 401,0|maaNode | response:
0|maaNode | Response {
0|maaNode | domain: null,0|maaNode | _events: {},0|maaNode | _eventsCount: 0,0|maaNode | _maxListeners: undefined,0|maaNode | res:
0|maaNode | IncomingMessage {
0|maaNode | _readableState: [Object],0|maaNode | readable: false,0|maaNode | domain: null,0|maaNode | _events: [Object],0|maaNode | _eventsCount: 4,0|maaNode | _maxListeners: undefined,0|maaNode | socket: [Object],0|maaNode | connection: [Object],0|maaNode | httpVersionMajor: 1,0|maaNode | httpVersionMinor: 1,0|maaNode | httpVersion: '1.1',0|maaNode | complete: true,0|maaNode | headers: [Object],0|maaNode | rawHeaders: [Array],0|maaNode | trailers: {},0|maaNode | rawTrailers: [],0|maaNode | upgrade: false,0|maaNode | url: '',0|maaNode | method: null,0|maaNode | statusCode: 401,0|maaNode | statusMessage: 'Unauthorized',0|maaNode | client: [Object],0|maaNode | _consuming: true,0|maaNode | _dumped: false,0|maaNode | req: [Object],0|maaNode | text: '{"errors": [{"code": "UNAUTHORIZED","detail": "The `Authorization` http header of your request was malformed. The header value is expected to be of the format \\"Bearer TOKEN\\" (without quotation marks),where TOKEN is to be replaced with your access token (e.g. \\"Bearer ABC123def456GHI789jkl0\\"). For more information,see https://docs.connect.squareup.com/api/connect/v2/#requestandresponseheaders. If you are seeing this error message while using one of our officially supported SDKs,please report this to developers@squareup.com.","category": "AUTHENTICATION_ERROR"}]}\n',0|maaNode | read: [Function] },0|maaNode | request:
0|maaNode | Request {
0|maaNode | domain: null,0|maaNode | _events: {},0|maaNode | _eventsCount: 0,0|maaNode | _agent: false,0|maaNode | _formData: null,0|maaNode | method: 'POST',0|maaNode | url: 'https://connect.squareup.com/v2/customers/search',0|maaNode | _header: [Object],0|maaNode | header: [Object],0|maaNode | writable: true,0|maaNode | _redirects: 0,0|maaNode | _maxRedirects: 5,0|maaNode | cookies: '',0|maaNode | qs: {},0|maaNode | _query: [],0|maaNode | qsRaw: [],0|maaNode | _redirectList: [],0|maaNode | _streamRequest: false,0|maaNode | _timeout: 60000,0|maaNode | _responseTimeout: 0,0|maaNode | _data: [Object],0|maaNode | protocol: 'https:',0|maaNode | host: 'connect.squareup.com',0|maaNode | _endCalled: true,0|maaNode | _callback: [Function],0|maaNode | res: [Object],0|maaNode | response: [Circular],0|maaNode | called: true },0|maaNode | req:
0|maaNode | ClientRequest {
0|maaNode | domain: null,0|maaNode | _eventsCount: 3,0|maaNode | output: [],0|maaNode | outputEncodings: [],0|maaNode | outputCallbacks: [],0|maaNode | outputSize: 0,0|maaNode | _last: true,0|maaNode | upgrading: false,0|maaNode | chunkedEncoding: false,0|maaNode | shouldKeepAlive: false,0|maaNode | useChunkedEncodingByDefault: true,0|maaNode | sendDate: false,0|maaNode | _removedConnection: false,0|maaNode | _removedContLen: false,0|maaNode | _removedTE: false,0|maaNode | _contentLength: 52,0|maaNode | _hasBody: true,0|maaNode | _trailer: '',0|maaNode | finished: true,0|maaNode | _headerSent: true,0|maaNode | _header: 'POST /v2/customers/search HTTP/1.1\r\nHost: connect.squareup.com\r\nAccept-Encoding: gzip,deflate\r\nUser-Agent: Square-Connect-Javascript/4.20200722.1\r\nAuthorization: Bearer EAAAEE117sYcLvlA_aBSNFjFk1q6OQivIvdP1LJoYmA-FxEsbd-pRC7NHz6PpFcz\r\nSquare-Version: 2020-07-22\r\nContent-Type: application/json\r\nAccept: application/json\r\nContent-Length: 52\r\nConnection: close\r\n\r\n',0|maaNode | _onPendingData: [Function: noopPendingOutput],0|maaNode | agent: [Object],0|maaNode | socketPath: undefined,0|maaNode | timeout: undefined,0|maaNode | path: '/v2/customers/search',0|maaNode | _ended: true,0|maaNode | aborted: undefined,0|maaNode | timeoutCb: null,0|maaNode | upgradeOrConnect: false,0|maaNode | parser: null,0|maaNode | maxHeadersCount: null,0|maaNode | [Symbol(outHeadersKey)]: [Object] },0|maaNode | text: '{"errors": [{"code": "UNAUTHORIZED",0|maaNode | body: { errors: [Array] },0|maaNode | files: undefined,0|maaNode | buffered: true,0|maaNode | headers:
0|maaNode | { date: 'Mon,24 Aug 2020 14:55:16 GMT',0|maaNode | 'frame-options': 'DENY',0|maaNode | 'x-frame-options': 'DENY',0|maaNode | 'x-content-type-options': 'nosniff',0|maaNode | 'x-xss-protection': '1; mode=block',0|maaNode | 'content-type': 'application/json',0|maaNode | 'content-length': '574',0|maaNode | 'strict-transport-security': 'max-age=631152000; includeSubDomains; preload',0|maaNode | connection: 'close' },0|maaNode | header:
0|maaNode | { date: 'Mon,0|maaNode | statusCode: 401,0|maaNode | status: 401,0|maaNode | statusType: 4,0|maaNode | info: false,0|maaNode | ok: false,0|maaNode | redirect: false,0|maaNode | clientError: true,0|maaNode | serverError: false,0|maaNode | error:
0|maaNode | { Error: cannot POST /v2/customers/search (401)
0|maaNode | at Response.toError (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/response.js:94:15)
0|maaNode | at ResponseBase._setStatusProperties (/home/ubuntu/maaWebsite/node_modules/superagent/lib/response-base.js:123:16)
0|maaNode | at new Response (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/response.js:41:8)
0|maaNode | at Request._emitResponse (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/index.js:752:20)
0|maaNode | at parser (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/index.js:916:38)
0|maaNode | at IncomingMessage.res.on (/home/ubuntu/maaWebsite/node_modules/superagent/lib/node/parsers/json.js:19:7)
0|maaNode | at emitNone (events.js:111:20)
0|maaNode | at IncomingMessage.emit (events.js:208:7)
0|maaNode | at endReadableNT (_stream_readable.js:1064:12)
0|maaNode | at _combinedTickCallback (internal/process/next_tick.js:138:11)
0|maaNode | status: 401,0|maaNode | path: '/v2/customers/search' },0|maaNode | created: false,0|maaNode | accepted: false,0|maaNode | noContent: false,0|maaNode | badRequest: false,0|maaNode | unauthorized: true,0|maaNode | notAcceptable: false,0|maaNode | forbidden: false,0|maaNode | notFound: false,0|maaNode | unprocessableEntity: false,0|maaNode | type: 'application/json',0|maaNode | links: {},0|maaNode | setEncoding: [Function: bound ],0|maaNode | redirects: [] } }
我是否可以解决某些问题,或者这是节点SDK的问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。