0

This code is changing fast and hard to get a handle on what works and what doesn't...

I was looking at this post: Have you used Google's Directory API?

Which is using the 1.4 library.

I installed the 1.6 API through nuget. However, the NativeApplicationClient and IAuthorizationState cannot be resolved. I was under the impression that I no longer needed the DotNetOpenAuth nuget package or the Google.Apis.Authentication package (which is where I believe they are resolved.

This is the complete and modified code I am playing with: (if you have a better example of creating users using the new API I'd like to see that!)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;

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


namespace GoogleAddUser
{
    class Program
    {
    static void Main(string[] args)
    {
        // Display the header and initialize the sample.
        //CommandLine.EnableExceptionHandling();
        Console.WriteLine("Create users in a google apps domain!");
        Console.WriteLine("by Jonas Bergstedt 2013");

        // Get the user data and store in user object
        Console.Write("Email: ");
        string userId = Console.ReadLine();

        Console.Write("Givenname: ");
        string GivenName = Console.ReadLine();

        Console.Write("Familyname: ");
        string FamilyName = Console.ReadLine();

        Console.Write("Password: ");
        string Password = Console.ReadLine();

        User newuserbody = new User();
        UserName newusername = new UserName();
        newuserbody.PrimaryEmail = userId;
        newusername.GivenName = GivenName;
        newusername.FamilyName = FamilyName;
        newuserbody.Name = newusername;
        newuserbody.Password = Password;

        // Register the authenticator.
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
        {
            ClientIdentifier = "<your clientId from Google APIs Console>",
            ClientSecret = "<your clientsecret from Google APIs Console>",
        };

        var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

        // Create the service.
        var service = new DirectoryService(new BaseClientService.Initializer()
        {
            Authenticator = auth,
            ApplicationName = "Create User",
            ApiKey = "<your API Key from Google APIs console> (not sure if needed)"
        });

        User results = service.Users.Insert(newuserbody).Execute();
        Console.WriteLine("User :" + results.PrimaryEmail + " is created");
        Console.WriteLine("Press any key to continue!");
        Console.ReadKey();
    }

    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
    {
        // Get the auth URL:
        IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() });
        state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
        Uri authUri = arg.RequestUserAuthorization(state);

        // Request authorization from the user (by opening a browser window):
        Process.Start(authUri.ToString());
        Console.WriteLine();
        Console.Write("Authorization Code: ");
        string authCode = Console.ReadLine();

        // Retrieve the access token by using the authorization code:
        return arg.ProcessUserAuthorization(authCode, state);
    }


}

}

Community
  • 1
  • 1
Zonus
  • 2,313
  • 2
  • 26
  • 48

1 Answers1

0

From release 1.6.0-beta we presented a new Google.Apis.Auth NuGet package (Google.Apis.Authentication which uses DNOA is obsolete!). You already installed that package, because all the new APIs have a reference to it. Take a look in our OAuth2 wiki page for more details about how to use the new OAuth2 flows (it's not magic anymore, now the flows actually make sense!)

I recommend you subscribing to our announcement blog and optionally to my personal blog to get more information about the client library. In our announcement blog we described the reason the new OAuth2 pacakge.

Hope it is helpful.

peleyal
  • 3,472
  • 1
  • 14
  • 25
  • I don't see a subscription link anywhere on the blog, is there somewhere else I need to go to subscribe to it? – Zonus Dec 03 '13 at 13:31
  • You can use http://feedly.com/ for example to manage your subscriptions to blogs (That's what I'm doing). You can also join our external group (https://groups.google.com/forum/?hl=en&fromgroups#!forum/google-api-dotnet-client). Information on a new release is published in the external group as well. – peleyal Dec 03 '13 at 14:49