2014-09-16 1 views
1

이미 프린터 목록을 가져 와서이 코드 "Google Cloud Print using C#"을 통해 Google 클라우드 프린트에 프린터 작업을 제출했지만 고객의 Google 비밀번호를 사용하여 프린터에 액세스 할 수 없습니다.C# - OAuth2 액세스 토큰을 사용하여 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" }; 

... 

나에게 줄 조언이 있습니까?

+0

스택 오버플로에 오신 것을 환영합니다. 여기에 질문하는 좋은 방법은 아닙니다. 문제를 해결하기 위해 지금까지 아무 것도 시도한 적이 있습니까? 사람들이 자신의 것을 보여줄 수 있도록 먼저 노력을 보여주십시오. [FAQ] (http://stackoverflow.com/tour), [How to Ask] (http://stackoverflow.com/help/how-to-ask) 및 [도움말 센터] (http : // stackoverflow)를 읽어보십시오. .com/help)을 시작으로 사용하십시오. –

+0

나는 해결했다. 누군가가 내 질문을 해결 한 방법을 볼 수있는 작은 프로젝트를 만들었고 누군가가 더 나은 해결책을 제시 할 수도있었습니다. 이제 나는 그것을 나눌 것입니다. – AppAndTouch

답변

4

작은 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

: 이것은 내 솔루션입니다.