问题
I have a user in my google admin console with the email dog@jopfre.com. I can auth successfully and add and delete users using the api. Now I am trying to update the user using the api. Here is a simplified version of my code:
const admin = google.admin({version: 'directory_v1', auth});
admin.users.update({
userKey: "dog@jopfre.com",
requestBody: {
primaryEmail: "cat@jopfre.com"
}
},(err, data) => {
console.log(err || data);
});
This returns json of the request and a 200 status.
The nearest example I can find in the docs is this:
admin.members.insert({
groupKey: 'my_group@example.com',
requestBody: { email: 'me@example.com' },
auth: jwt
}, (err, data) => {
console.log(err || data);
});
So it looks pretty similar to me.
I have tried with and without quotation marks on the requestBody key and have also tried updating different key values like givenName
and suspended
. I'm guessing my request is malformed somehow but I can't work out how as no error is returned.
Any clue or ideas of what to try next?
Here are some of the more relevant lines from the returned json:
status: 200,
params: { requestBody: { primaryEmail: 'cat@jopfre.com' } },
_hasBody: true,
header: 'PUT /admin/directory/v1/users/dog@jopfre.com?requestBody%5BprimaryEmail%5D=cat%40jopfre.com HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nContent-Type: application/x-www-form-urlencoded\r\nAuthorization: Bearer ya29.GlwXBi796knRrOTbzvJ1ihzBaQqHKk3HYA9-3pxUgCxaCvPKxZLYGRrghq_RcFHbZYqyKEqUV6yOWusBui2Vh1DLd50MsKQ5o4MoqzutVr8P280ULY2cYzSYLtGOyw\r\nUser-Agent: google-api-nodejs-client/1.6.1\r\nHost: www.googleapis.com\r\nConnection: close\r\nContent-Length: 0\r\n\r\n',
path: '/admin/directory/v1/users/dog@jopfre.com?requestBody%5BprimaryEmail%5D=cat%40jopfre.com',
responseUrl: 'https://www.googleapis.com/admin/directory/v1/users/dog@jopfre.com?requestBody%5BprimaryEmail%5D=cat%40jopfre.com',
_requestBodyLength: 0,
Not sure if the requestBodyLength should be 0, that seems off.
回答1:
Using resource
instead of requestBody
works in v33.0.0 of googleapis.
const admin = google.admin({version: 'directory_v1', auth});
admin.users.update({
userKey: "dog@jopfre.com",
resource: {
primaryEmail: "cat@jopfre.com"
}
},(err, data) => {
console.log(err || data);
});
回答2:
Effective release of googleapis
version 30.0.0 resource
and requestBody
are equally accepted.
Below are working examples for users.insert
, users.list
, users.update
, users.get
and users.delete
functions, all tested with googleapis
version 30.0.0
async function insertUser(auth) {
const service = google.admin({version: 'directory_v1', auth});
console.log("Inserting user");
const response = await service.users.insert({
"requestBody":{
"name": {
"familyName": "Friends",
"givenName": "John Smith",
},
"password": "**********",
"primaryEmail": "j.smith@jopfre.com",
}
})
// Log the results here.
console.log(`status: ${response.status}\nprimary email: ${response.data.primaryEmail}\nupdated familyName: ${response.data.name.fullName}`)
console.log("\n"); // insert a line break.
}
async function listUsers(auth) {
console.log('Listing users')
const service = google.admin({version: 'directory_v1', auth});
const response = await service.users.list({
customer: 'my_customer',
maxResults: 150,
orderBy: 'email',
})
const users = response.data.users;
if (users.length) {
console.log('Users:');
users.forEach((user) => {
console.log(`${user.primaryEmail} -(${user.name.fullName})`);
});
} else {
console.log('No users found.');
}
console.log("\n"); // insert a line break.
}
async function updateUserInfo(auth) {
console.log('Updating user info')
const service = google.admin({version: 'directory_v1', auth});
const response = await service.users.update({
"userKey": "j.smith@jopfre.com",
"requestBody": {
"name": {
"familyName": "Work"
},
"primaryEmail": "john.smith@jopfre.com"
}
})
// Log the results here.
console.log('User info is updated successfully')
console.log(`status: ${response.status}, prime email: ${response.data.primaryEmail} updated familyName: ${response.data.name.familyName}`)
for (i = 0; i < response.data.emails.length; i++) {
console.log(`address: ${response.data.emails[i]["address"]}`)
}
console.log("\n"); // insert a line break.
}
async function getUserMeta(auth) {
console.log('Getting user info')
const service = google.admin({version: 'directory_v1', auth});
const response = await service.users.get({
"userKey" : "j.smith@jopfre.com"
})
console.log('User info is obtained successfully')
console.log(`primary email: ${response.primaryEmail}, full name: ${response.data.name.fullName}`)
console.log("\n"); // insert a line break.
}
async function deleteUser(auth) {
console.log('Deleting user')
const service = google.admin({version: 'directory_v1', auth});
const response = await service.users.delete({
"userKey" : "j.smith@jopfre.com"
})
if (response.data == "") {
console.log("User is deleted successfully");
}
}
来源:https://stackoverflow.com/questions/52316460/update-user-using-google-admin-directory-api-returns-200-but-does-not-update-ma