1

我正在尝试使用 .NET 的 Google Directory API 库来维护域的电子邮件地址。最新的库是 google-admin-directory_v1-rev6-csharp-1.4.0-beta。到目前为止,我得到的最好和最远的是收到 403 错误(未授权访问此资源/api)。

有没有人成功使用过它?如果是这样,您能否分享一些代码、提示或技巧?

4

1 回答 1

3

我已经使用它并在创建控制台应用程序方面取得了一些成功。目前我正试图找到一种方法来跳过授权码的复制/粘贴。不要忘记在您的 API 控制台中打开 API 访问。我将向您展示一个小样本:

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

using DotNetOpenAuth.OAuth2;

using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Samples.Helper;
using Google.Apis.Services;
using Google.Apis.Util;
using Google.Apis.Admin.directory_v1;
using Google.Apis.Admin.directory_v1.Data;



namespace Bergstedts.CreateNewUser
{
    public 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);
    }
}

}

于 2013-07-23T08:27:47.457 回答