【问题标题】:C# - How do I retrieve google cloud printers with OAuth2 Access Token?C# - 如何使用 OAuth2 访问令牌检索谷歌云打印机?
【发布时间】:2014-11-10 02:26:08
【问题描述】:

我已经获得了打印机列表,并通过此代码“Google Cloud Print using C#”向 Google 云打印提交了打印机作业,但我无法使用客户的 Google 密码访问他们的打印机。

现在我正在实施 oauth2 身份验证,并且我已经获得了测试帐户的日历和 Google 云打印的访问权限,但现在我不明白如何检索打印机列表以及如何使用此授权令牌发布打印机的作业.

对于 oauth2,我已经下载了这个示例并且运行良好“https://github.com/nanovazquez/google-calendar-sample”。

如果您有权使用 Google 云打印,只需在 _scopes 成员中添加“https://www.googleapis.com/auth/cloudprint”即可。

using System.Web;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Calendar.v3;
using Google.Apis.Util;

namespace GoogleApiUtils
{
    public static class GoogleAuthorizationHelper
    {
        private static string _clientId = ConfigurationManager.AppSettings["ClientId"];
        private static string _clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
        private static string _redirectUri = ConfigurationManager.AppSettings["RedirectUri"];
        private static string[] _scopes = new[] { CalendarService.Scopes.Calendar.GetStringValue(), "https://www.googleapis.com/auth/cloudprint" };

...

有没有人可以给我一些建议?

【问题讨论】:

  • 欢迎来到 Stack Overflow。这不是在这里提问的好方法。到目前为止,您是否尝试过任何方法来解决您的问题?首先展示你的努力,这样人们就可以展示他们的努力。请先阅读FAQHow to Askhelp center
  • 我已经解决了。我创建了一个小项目,因此任何人都可以看到我是如何解决我的问题的,也许有人可以提供更好的解决方案。现在我要分享它。

标签: oauth-2.0 cloud token access-token printers


【解决方案1】:

我做了一个小的 C# MVC 项目。我检索了 OAuth2 访问令牌并将其用于获取 Google 云打印机的列表。我不知道是否有其他方法可以做到这一点。我希望这可以帮助某人。

我最大的问题是如何在访问令牌过期时刷新它。这是我的解决方案:

    public async Task<ActionResult> PrintersListAsync(CancellationToken cancellationToken)
    {           
        cancellationToken.ThrowIfCancellationRequested();

        List<CloudPrinter> model = new List<CloudPrinter>();

        try
        {
             ViewBag.Message = "Your printers.";

            var result = await new AuthorizationCodeMvcApp(this, new AppAuthFlowMetadata()).AuthorizeAsync(cancellationToken);

            if (result.Credential == null)
            {
                // Open Google page for authorization
                return new RedirectResult(result.RedirectUri);
            }

            //Check if token is expired
            if (result.Credential.Token.IsExpired(Google.Apis.Util.SystemClock.Default)) {
                //renew token
                Boolean tokenRefreshed = await result.Credential.RefreshTokenAsync(cancellationToken);

                if (!tokenRefreshed)
                {                       
                    //Refresh token failed!
                    return new RedirectResult(result.RedirectUri);
                }
            }

            //Ok, now we have rights to access to user printers
            GoogleCloudPrint cloudPrint = new GoogleCloudPrint(result.Credential, String.Empty);

            //Get printers list
            var printers = cloudPrint.Printers;
            if (printers.success)
            {
                model = printers.printers;
            }

            return View(model);
        }
        catch (Exception ex)
        {
            ViewBag.Message = "Exception: " + ex.Message;

            return View(model); 
        }           
    }

Link to C# MVC project

感谢任何改进。

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 2019-02-02
    • 2020-12-05
    • 2016-04-15
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多