Please help me with the step-by-step process to create a Postman API call request to deactivate the Google Workspace user account
Can you provide more details and context here, please.
Hi @danny-dainton,
I have successfully created a Postman request for deleting a Google Workspace user, as shown in the attached screenshot below.
However, I now need assistance in modifying the Postman call to deactivate the user account instead of deleting it. Please guide how to achieve this. Thank you.
Which documentation are you following?
What are the details of the request needed to deactivate a user?
Can you deactivate a user via the API? Is there a different way of doing that but changing their role of something different.
There’s no real context about why you need to do this or how this fits into something you’re working on.
I am just following online documentation.
can you please help me with the related documents?
Yes, it’s working in my Python code. but I am required in Curl code for a successful response.
Which documentation, you’re going to need to provide a link
What does the python code look like?
below is the Python code, which is working for User deactivation… I have used json file for that authentication token.
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
If modifying these scopes, delete the file token.json.
SCOPES = [“https://www.googleapis.com/auth/admin.directory.user”]
def main():
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
service = build("admin", "directory_v1", credentials=creds)
# Deactivate users
users_to_deactivate = ["test123@21kschool.school"]
for user_email in users_to_deactivate:
try:
# Get the user resource
user_resource = service.users().get(userKey=user_email).execute()
# Update the 'suspended' field to True (deactivate)
user_resource["suspended"] = True
# Update the user
service.users().update(userKey=user_email, body=user_resource).execute()
print(f"User {user_email} deactivated successfully.")
except Exception as e:
print(f"Error deactivating user {user_email}: {e}")
if name == ‘main’:
main()
This topic was automatically closed after 90 days. New replies are no longer allowed.