首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >你用过谷歌的目录API吗?

你用过谷歌的目录API吗?
EN

Stack Overflow用户
提问于 2013-07-04 00:30:32
回答 1查看 2.5K关注 0票数 1

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-23 16:27:47

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

代码语言:javascript
复制
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);
    }
}

}

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17453391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档