C# - 如何使用OAuth2访问令牌检索谷歌云打印机?

时间:2014-09-16 11:01:46

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

我已经有了打印机列表并通过此代码向Google Cloud Print提交了打印机作业" Google Cloud Print using C#"但我不能使用谷歌客户的密码来访问他们的打印机。

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

对于oauth2,我已经下载了这个例子,效果非常好" https://github.com/nanovazquez/google-calendar-sample"。

对于Google云打印的版权,只需添加" https://www.googleapis.com/auth/cloudprint"在_scopes会员。

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" };

...

有人有建议给我吗?

1 个答案:

答案 0 :(得分:4)

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

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

    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

赞赏任何改进。