Mike Preston
Mike Preston

Reputation: 61

Google Directory API and reset user passwords through .net

Well, after a lot of help from posts on this site and others I've finally got my .net application connected and authenticating to my google apps instance! Hooray! I can search and get user details to my hearts delight utilizing a service account that I've delegated access to.

That said, I can't seem to be able to update a user's password. I get no errors, no response, nothing - it just doesn't work.

Here's what I got so far, like i said, i get user details great, just can't change that password!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Google.Apis.Admin.Directory;


String serviceAccountEmail = "[email protected]";
var certificate = new X509Certificate2(@"c:\path\to\my\p12key.p12", "mysecret", X509KeyStorageFlags.Exportable);


ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
  User="[email protected]",

  Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser }
}.FromCertificate(certificate));



var dirservice = new DirectoryService(new BaseClientService.Initializer()
{
  HttpClientInitializer = credential,
  ApplicationName = "MyProjectName",
});

User user = dirservice.Users.Get("[email protected]").Execute();
Console.WriteLine(" email: " + user.PrimaryEmail);
Console.WriteLine(" last login: " + user.LastLoginTime);

user.Password = "newpassword";
dirservice.Users.Update(user, "[email protected]");

The last two lines are the ones I expect would change the users password - but it doesn't.

Any help here would be much appreciated! Thanks so much!

Upvotes: 0

Views: 1719

Answers (1)

Mike Preston
Mike Preston

Reputation: 61

Nevermind!

I forgot the .Execute()

it works with .execute()

user.Password = "newpassword";
dirservice.Users.Update(user, "[email protected]").Execute();

Thanks!

In case anyone wants the complete write-up I did about how to configure the project, developer console, service account etc it's right here!

http://blog.mwpreston.net/2014/10/17/google-admin-sdk-apis-and-net-integration-changing-a-users-password/

Upvotes: 2

Related Questions