桌面应用程序的多个 Google 访问权限

Posted

技术标签:

【中文标题】桌面应用程序的多个 Google 访问权限【英文标题】:Multiple Google access permissions for a desktop application 【发布时间】:2015-09-05 12:01:41 【问题描述】:

我正在用 C# 编写一个桌面应用程序,它应该能够访问 Google Apps“帐户”上的所有用户,并为每个用户检索日历事件。我已将日历 API 和 Admin SDK 添加到我的“项目”中。

这两种方法(如下)都可以单独使用,但是当我想为这两个 API 授权我的应用时,我会收到以下权限错误。

权限不足 [403]

invalid_grant", 描述:"令牌已被吊销。

这让我想知道是否可以在应用程序启动时请求所有权限,而不是单独授权“功能”?

    static string[] CalendarScopes = CalendarService.Scope.CalendarReadonly ;
    static string[] DirectoryScopes =     DirectoryService.Scope.AdminDirectoryUserReadonly ;

    private static void GoogleCalendar()
    
        UserCredential credential;

        using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        
            string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                CalendarScopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        

        // Create Google Calendar API service.
        var service = new CalendarService(new BaseClientService.Initializer()
        
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        );

        // Define parameters of request.
        EventsResource.ListRequest request = service.Events.List("primary");
        request.TimeMin = DateTime.Now;
        request.ShowDeleted = false;
        request.SingleEvents = true;
        //request.MaxResults = 10;
        request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

        // List events.
        Events events = request.Execute();
        Console.WriteLine("Upcoming events:");
        if (events.Items != null && events.Items.Count > 0)
        
            foreach (var eventItem in events.Items)
            
                string when = eventItem.Start.DateTime.ToString();
                if (String.IsNullOrEmpty(when))
                
                    when = eventItem.Start.Date;
                
                Console.WriteLine("0 (1)", eventItem.Summary, when);
            
        
        else
        
            Console.WriteLine("No upcoming events found.");
        
        Console.Read();
    

    private static void GoogleDirectory()
    
        UserCredential credential;

        using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        
            string credPath = Environment.GetFolderPath(
                Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                DirectoryScopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        

        // Create Directory API service.
        var service = new DirectoryService(new BaseClientService.Initializer()
        
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        );

        // Define parameters of request.
        UsersResource.ListRequest request = service.Users.List();
        request.Customer = "my_customer";
        request.MaxResults = 10;
        request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;

        // List users.

        IList<User> users = null;

        try
        
            users = request.Execute().UsersValue;
        
        catch (Exception ex)
        
            throw;
        


        Console.WriteLine("Users:");
        if (users != null && users.Count > 0)
        
            foreach (var userItem in users)
            
                Console.WriteLine("0 (1)", userItem.PrimaryEmail,
                    userItem.Name.FullName);
            
        
        else
        
            Console.WriteLine("No users found.");
        
        Console.Read();
    

【问题讨论】:

您遇到什么错误?是的,您可以请求具有应用程序所需的所有范围的访问令牌。虽然不推荐这样做。 我已经更新了我的问题,但有例外。为什么不建议对多个范围使用访问令牌? 【参考方案1】:

尤里卡!我想通了,怎么解决这个问题,其实很简单。

解决方案是“合并”这些方法。这意味着,我只需使用访问令牌检查一次凭据。我还将目录添加到范围数组中。

    static string[] Scopes =  CalendarService.Scope.CalendarReadonly, DirectoryService.Scope.AdminDirectoryUserReadonly ;

    private static void GoogleThis()
    
        UserCredential credential;

        using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        
            string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        

        // Create Google Calendar API service.
        var eventService = new CalendarService(new BaseClientService.Initializer()
        
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        );

        // Define parameters of request.
        EventsResource.ListRequest eventRequest = eventService.Events.List("primary");
        eventRequest.TimeMin = DateTime.Now;
        eventRequest.ShowDeleted = false;
        eventRequest.SingleEvents = true;
        //request.MaxResults = 10;
        eventRequest.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

        // List events.
        Events events = eventRequest.Execute();
        Console.WriteLine("Upcoming events:");
        if (events.Items != null && events.Items.Count > 0)
        
            foreach (var eventItem in events.Items)
            
                string when = eventItem.Start.DateTime.ToString();
                if (String.IsNullOrEmpty(when))
                
                    when = eventItem.Start.Date;
                
                Console.WriteLine("0 (1)", eventItem.Summary, when);
            
        
        else
        
            Console.WriteLine("No upcoming events found.");
        
        // Create Directory API service.
        var dirService = new DirectoryService(new BaseClientService.Initializer()
        
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        );

        // Define parameters of request.
        UsersResource.ListRequest dirRequest = dirService.Users.List();
        dirRequest.Customer = "my_customer";
        dirRequest.MaxResults = 10;
        dirRequest.OrderBy = UsersResource.ListRequest.OrderByEnum.Email;

        // List users.
        IList<User> users = null;
        try
        
            users = dirRequest.Execute().UsersValue;
            Console.WriteLine("Users:");
            if (users != null && users.Count > 0)
            
                foreach (var userItem in users)
                
                    Console.WriteLine("0 (1)", userItem.PrimaryEmail,
                        userItem.Name.FullName);
                
            
            else
            
                Console.WriteLine("No users found.");
            
        
        catch (Exception ex)
        
            throw;
        
    

【讨论】:

以上是关于桌面应用程序的多个 Google 访问权限的主要内容,如果未能解决你的问题,请参考以下文章

vmware桌面云 有个软件桌面池,让所有桌面云终端用户用不同权限去访问安装桌面池的软件

google 的 firebase 可以与 java 桌面应用程序一起使用吗?

如何建立远程桌面服务Web访问端口

为了使得多个用户能远程登陆,我装了终端服务,之后,远程桌面不能登陆,提示没有“终端服务器用户访问权限”

如何在代码中配置我的 windows 服务以访问桌面?

将 Google 地球嵌入桌面应用程序而不是网站。 QT是一个真正的选择吗?