Reputation: 425
I am currently following Google's Python Quickstart for Directory API and experimenting with different ways of using the REST API's. The tutorial is designed to run within the Command Line and authenticates with O Auth 2.0 by creating a I have managed to change the code of the tutorial so that I can insert a user into a group that is already existing, by passing a groupKey of the group I am wishing to insert a User into and the body containing the email of the User I am adding, however, I cannot get Create a Group to work.
The error that I am getting is:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?alt=json returned "Insufficient Permission">
I am almost positive that my permissions within the Google Cloud Project are correct as I am a editor, and within G Suite I am a Super Admin. At first I thought I might of been pointing to the wrong Scope, but it appears that that is the recomended scope for creating the Group.
Could somebody please aid me in getting a group created, as I am very new to REST API's and wish to improve my own skills and understanding. Whether there has been an area that I simply misunderstood and need to go back and read on.
I have posted my code below:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/admin.directory.group'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'generic-app-name' # removed for example - imagine the name is included
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'admin-directory_v1-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('admin', 'directory_v1', http=http)
print('Creating Group For Testing')
test = {
"email" : "[email protected]", # Fake email for example
"name" : "Test Group",
"description" : "Just testing here."
}
group = service.users().insert(body = test).execute()
return group
if __name__ == '__main__':
main()
Thank you for taking the time to read the post and I hope to hear back from some of you soon,
A hopeful programmer.
Upvotes: 1
Views: 2014
Reputation: 1028
You can follow the link The Google Directory Groups API for python for create a group in Google Directory.
Upvotes: 2
Reputation: 425
I found the solution after looking at the Error Logs within the Google Cloud project, rather a simple fix. Before:
group = service.users().insert(body = test).execute()
After:
group = service.groups().insert(body = test).execute()
Here you can see that I have changed service.users to service.groups.
I cannot believe I let such a simple mistake get past me, but leaving on here incase this helps others.
Thank you
Upvotes: 1