我正在使用 Google 的 Business API 从 Node.js 应用程序添加位置,但遇到以下错误:
{
"error": "Request contains an invalid argument."
}
我正在使用
googleapis
npm 包,并尝试注释掉所有可选字段,只在请求正文中保留必填字段。但是,我仍然遇到错误。这是我的代码:
app.post('/add-location', async (req, res) => {
const { location, tokens } = req.body;
oauth2Client.setCredentials(tokens);
const mybusinessbusinessinformation = google.mybusinessbusinessinformation('v1');
try {
const response = await mybusinessbusinessinformation.accounts.locations.create({
parent: 'accounts/' + process.env.ACCOUNT_ID,
validateOnly: true,
requestId: uuidv4(),
requestBody: {
// "name": "EVgo Charging Station",
// "languageCode": "en",
// "storeCode": "EVG123",
"title": "EVgo Charging Station",
// "phoneNumbers": {
// "primaryPhone": "+1 123-456-7890"
// },
// "categories": {
// "primaryCategory": {
// "name": "categories/gcid:electric_vehicle_charging_station",
// "displayName": "Electric vehicle charging station",
// },
// },
// "storefrontAddress": {
// "regionCode": "US",
// "locality": "Durham",
// "postalCode": "27707",
// "addressLines": ["4711 Hope Valley Rd", "Woodcroft Shopping Center"]
// },
// "websiteUri": "https://evgo.com",
// "regularHours": {
// "periods": [
// {
// "openDay": "MONDAY",
// "openTime": {
// "hours": 0,
// "minutes": 0
// },
// "closeDay": "SUNDAY",
// "closeTime": {
// "hours": 23,
// "minutes": 59
// }
// }
// ]
// },
// "specialHours": {
// "specialHourPeriods": [
// {
// "startDate": {
// "year": 2024,
// "month": 12,
// "day": 25
// },
// "endDate": {
// "year": 2024,
// "month": 12,
// "day": 25
// },
// "closed": true
// }
// ]
// },
// "labels": [
// "EV Charging",
// "Fast Charging"
// ],
// "latlng": {
// "latitude": 35.951033,
// "longitude": -78.956228
// },
// "openInfo": {
// "status": "OPEN",
// "canReopen": true
// },
// "metadata": {
// "mapsUri": "https://maps.google.com/?q=EVgo+Charging+Station",
// "newReviewUri": "https://maps.google.com/review?place_id=ChIJzxbB6Gs7bIcRZZrPTeyb-0A"
// },
"profile": {
"description": "Fast and reliable electric vehicle charging station."
},
// "relationshipData": {
// "parentChain": "EVgo"
// },
// "moreHours": [
// {
// "hoursTypeId": "DRIVE_THROUGH",
// "periods": [
// {
// "openDay": "MONDAY",
// "openTime": {
// "hours": 0,
// "minutes": 0
// },
// "closeDay": "SUNDAY",
// "closeTime": {
// "hours": 23,
// "minutes": 59
// }
// }
// ]
// }
// ],
},
auth: oauth2Client
});
console.log(response)
res.send(response.data);
} catch (error) {
console.error(error)
res.status(400).send({ error: error.message });
}
});
这是详细日志
GaxiosError: Request contains an invalid argument.
at Gaxios._request (/Users/workstation/Programming/Clients/COS/gmb-microservice/node_modules/gaxios/build/src/gaxios.js:142:23)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async OAuth2Client.requestAsync (/Users/workstation/Programming/Clients/COS/gmb-microservice/node_modules/google-auth-library/build/src/auth/oauth2client.js:429:18)
at async /Users/workstation/Programming/Clients/COS/gmb-microservice/server.js:52:26 {
config: {
url: 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/102094468378068712054/locations?validateOnly=true&requestId=54e30089-a836-441c-8eb0-bdd936c0a979',
method: 'POST',
apiVersion: '',
userAgentDirectives: [ [Object] ],
paramsSerializer: [Function (anonymous)],
data: { title: 'EVgo Charging Station', profile: [Object] },
headers: {
'x-goog-api-client': 'gdcl/7.2.0 gl-node/20.8.0',
'Accept-Encoding': 'gzip',
'User-Agent': 'google-api-nodejs-client/7.2.0 (gzip)',
Authorization: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
'Content-Type': 'application/json'
},
params: {
validateOnly: true,
requestId: '54e30089-a836-441c-8eb0-bdd936c0a979'
},
validateStatus: [Function (anonymous)],
retry: true,
body: '{"title":"EVgo Charging Station","profile":{"description":"Fast and reliable electric vehicle charging station."}}',
responseType: 'unknown',
errorRedactor: [Function: defaultErrorRedactor],
retryConfig: {
currentRetryAttempt: 0,
retry: 3,
httpMethodsToRetry: [Array],
noResponseRetries: 2,
retryDelayMultiplier: 2,
timeOfFirstRequest: 1722445004419,
totalTimeout: 9007199254740991,
maxRetryDelay: 9007199254740991,
statusCodesToRetry: [Array]
}
},
response: {
config: {
url: 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/102094468378068712054/locations?validateOnly=true&requestId=54e30089-a836-441c-8eb0-bdd936c0a979',
method: 'POST',
apiVersion: '',
userAgentDirectives: [Array],
paramsSerializer: [Function (anonymous)],
data: [Object],
headers: [Object],
params: [Object],
validateStatus: [Function (anonymous)],
retry: true,
body: '{"title":"EVgo Charging Station","profile":{"description":"Fast and reliable electric vehicle charging station."}}',
responseType: 'unknown',
errorRedactor: [Function: defaultErrorRedactor]
},
data: { error: [Object] },
headers: {
'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
'cache-control': 'private',
'content-encoding': 'gzip',
'content-type': 'application/json; charset=UTF-8',
date: 'Wed, 31 Jul 2024 16:56:44 GMT',
server: 'ESF',
'transfer-encoding': 'chunked',
vary: 'Origin, X-Origin, Referer',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-xss-protection': '0'
},
status: 400,
statusText: 'Bad Request',
request: {
responseURL: 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/102094468378068712054/locations?validateOnly=true&requestId=54e30089-a836-441c-8eb0-bdd936c0a979'
}
},
error: undefined,
status: 400,
code: 400,
errors: [
{
message: 'Request contains an invalid argument.',
domain: 'global',
reason: 'badRequest'
}
],
[Symbol(gaxios-gaxios-error)]: '6.7.0'
}
另外,googleapis npm 包中是否有关于 Google Business API 的详细文档?
我按照以下 URL 创建请求正文
在添加一些额外的日志后,我还确定了一些其他必需的项目。
组织帐户下似乎无法添加位置。因此,我在组织下创建了一个位置组帐户并使用该位置组帐户 ID。终于成功了。
这里有详细的解释。 https://github.com/googleapis/google-api-nodejs-client/issues/3530