2015-04-05 71 views
0

我正在做一些基于Web的应用程序,显示数据库条目中的用户日历。在此Web应用程序中,用户可以通过JavaScript库对其Google日历帐户进行身份验证。然后我想与我的桌面服务共享此访问权限。桌面服务是用C#编写的。如何在Google API中使用Web应用程序生成的令牌填充本机应用程序凭据?

是否有人知道如何在C#中填充CalendarService类,并拥有以下代码?

的Javascript:

function auth() { 
      var config = { 
       'client_id': "myID", 
       'scope': "https://www.googleapis.com/auth/calendar" 
      }; 
      gapi.auth.authorize(config, function() { 
       console.log('login complete'); 
       console.log(gapi.auth.getToken()); 
      }); 
     } 

那么在Windows服务

var tokenResponse = new TokenResponse 
{ 
     AccessToken = tokenCopiedFromJavascriptObject, 
     Issued = propertyCopiedFromJavascriptObject, 
     TokenType = "Bearer", 
     Scope = CalendarService.Scope.Calendar, 
     RefreshToken = "3600" 
}; 

var authorizationCodeFlowInitializer = new GoogleAuthorizationCodeFlow.Initializer 
     { 
      ClientSecrets = new ClientSecrets() 
      { 
       ClientId = myIdForDesktopApp, 
       ClientSecret = ClientSecretForDestopApp 
      }, 
      Scopes = new[] { CalendarService.Scope.Calendar }, 
      Clock = Google.Apis.Util.SystemClock.Default 
     }; 
     var authorizationCodeFlow = new GoogleAuthorizationCodeFlow(authorizationCodeFlowInitializer); 
     _credentials = new UserCredential(authorizationCodeFlow, _calendarId, tokenResponse); 

     _service = new CalendarService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = _credentials, 
      ApplicationName = "Fast and Easy Reservation System" 
     }); 
     const string meeting = "Some fancy meeting"; 
     var e = _service.Events.QuickAdd("primary", meeting).Execute(); 
    } 

我的C#代码是否有希望得到这个工作?例如,我在Google API项目中有2个应用程序(网页和桌面)。这些都在一个项目中,所以我认为它应该作为一个混合应用程序工作,他们应该共享一个授权令牌。通过JS AUTH函数返回

对象具有以下属性: 的access_token CLIENT_ID expires_at expires_in issued_at RESPONSE_TYPE 范围 token_type

回答

0

我做的Controler在ASP.NET MVC应用程序来处理JavaScript代码和将令牌发送到我的数据库。 为了避免不兼容的应用程序类型(Web和本机),我的Windows服务使用来自Web类型应用程序的client_secrets.json文件。为了精确地控制应用程序的流程,并以某种方式欺骗本地服务是Web应用程序,我只从client_secrets.json(web)加载了client_id和client_secret。

  var flow = new GoogleAuthorizationCodeFlow(
      new GoogleAuthorizationCodeFlow.Initializer 
      { 
       ClientSecrets = new ClientSecrets() 
       { 
        ClientId = "some-numbersAndLettersxxxxxxxxxx.apps.googleusercontent.com", 
        ClientSecret = "xxxxxxxxxxxxxx" 
       } 
      }); 

然后,我在我的应用程序中从数据库中加载了令牌并分配给它。

var token = new TokenResponse 
     { 
      RefreshToken = "ya29.UAH2llQasSADfga32fWay3aqi1pb0AUwAWy_WzbPNyNkYCRPhe5_zBbEG94TZafV8pBMHzC8Q" 
     }; 

经过充分准备的流程和令牌之后,我能够创建凭证对象。

var credential = new UserCredential(flow, clientId, token); 

现在,本机应用程序伪装成网络应用程序,它可以访问所有的用户数据,无需额外的同意屏幕。

相关问题