1
votes

I have already got printers list and submitted a printer job to Google Cloud Print through this code "Google Cloud Print using C#" but I can not use Google's passwords of my customers for access theirs printers.

Now I am implementing the oauth2 authentication and I have gained the access to Calendar and to Google Cloud Print of a testing account but now I do not understand how to retrieve the list of printers and how post a printer's job with this authorization token.

For oauth2 i have download this example and works very well "https://github.com/nanovazquez/google-calendar-sample".

For have rights to Google Cloud Print just add "https://www.googleapis.com/auth/cloudprint" at _scopes member.

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

...

Does anyone have some advice to give me?

1
Welcome to Stack Overflow. This is not a good way to ask a question here. Did you try anything so far to solve your problem? Show your effort first so people might show theirs. Please read FAQ, How to Ask and help center as a start. - Nahuel Ianni
I have resolved. I have created a small project so anyone can see how I have solved my question and maybe someone could give a better solution. Now I am going to share it. - AppAndTouch

1 Answers

4
votes

I made a small C# MVC project. I retrieved the OAuth2 Access Token and I used it for get list of Google Cloud Printer. I do not know if there are other method for do that. I hope this could help someone.

My big problem was been how refresh access token when it expires. This is my solution:

    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

Any improvement is appreciated.