c# - Unable to Create User using Google Admin SDK - Exception 401 Unauthorized -


i trying populate users in c# application. however, keep getting exception:

{"error occurred while sending direct message or getting response."} [dotnetopenauth.messaging.protocolexception]: {"error occurred while sending direct message or getting response."} data: {system.collections.listdictionaryinternal} helplink: null innerexception: {"the remote server returned error: (401) unauthorized."} message: "error occurred while sending direct message or getting response." source: "google.apis" stacktrace: "   @ google.apis.requests.clientservicerequest`1.execute() in c:\\code.google.com\\google-api-dotnet-client\\default\\tools\\buildrelease\\bin\\release\\release140\\default\\src\\googleapis\\apis\\requests\\clientservicerequest.cs:line 88\r\n   @ googleaccountspopulation.directoryserviceclient.createuser(user user) in e:\\googleaccountspopulation\\googleaccountspopulation\\googleaccountspopulation\\directoryserviceclient.cs:line 70\r\n   @ googleaccountspopulation.frmgoogleaccountspopulation.btnpopulateaccounts_click(object sender, eventargs e) in e:\\googleaccountspopulation\\googleaccountspopulation\\googleaccountspopulation\\populateform.cs:line 449" targetsite: {tresponse execute()} 

the exception being raised on line:

userserviceclient.createuser(user); 

here codes service:

directoryservicespecs userservicespecs = new directoryservicespecs() {     certificatefilepath = path.getdirectoryname(application.executablepath) + "\\certificates\\" + gappsschool.certificatefile,     privatekey = gappsschool.privatekey,     serviceaccountid = gappsschool.serviceaccountid,     serviceaccountuser = gappsschool.serviceaccountuser,     domain = gappsschool.emaildomain,     servicescope = directoryservice.scopes.admindirectoryuser.getstringvalue() }; directoryserviceclient userserviceclient = new directoryserviceclient(userservicespecs);  user user = new user(); user.name = new username(); if (usernames.length > 1) {     user.name.familyname = lmsuser.name.replace(usernames[0], "").trim();     user.name.givenname = usernames[0]; } else {     user.name.familyname = "n.a.";     user.name.givenname = usernames[0]; } user.name.fullname = lmsuser.name; user.password = lmsuser.password; user.primaryemail = lmsuser.emailaccount + "@" + gappsschool.emaildomain; if (properties.settings.default.lmspasswardhashalgorithm.trim() != string.empty)     user.hashfunction = "md5";  user = userserviceclient.createuser(user); trackentries.add(new trackentry() {     emailaccount = lmsuser.emailaccount,     schoolname = gappsschool.name,     status = "success",     error = "" }); log.info("successfully created user \"" + lmsuser.emailaccount + "\" (" + lmsuser.id.tostring() + ", " + gappsschool.name + ").");   userserviceclient.createuser(user); 

authorization

in google apps domains, domain administrator can grant third-party applications domain-wide access users' data — referred domain-wide delegation of authority. delegate authority way, domain administrators can use service accounts oauth 2.0.

here list of scopes available in directory api.

to request access using oauth 2.0, application needs scope information, information google supplies when register application (such client id , client secret).

try checking how perform google apps domain-wide delegation of authority. receiving exception 401 unauthorized maybe because service account not authorized create user (wrong scope used or scope used not added), based emily's answer - must impersonate administrator account (note: administrator can create users).


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -