Google AUTH API应用程序类型,它有多重要?

时间:2014-12-04 04:12:57

标签: google-api google-api-dotnet-client gmail-api google-authentication

我一直在使用Google提供的.NET库来修改身份验证。

我们同时拥有桌面和Web应用程序方面,我们希望实现的是在桌面或Web端对ONCE进行身份验证,并存储刷新令牌,并在Web端和桌面端。

所以情况是这样的,在桌面方面,当没有保存现有的AccessToken和RefreshToken时,我们会要求用户通过以下代码进行身份验证:

            using (var stream = new FileStream("client_secrets_desktop.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.GmailCompose },
                "someemail@gmail.com", CancellationToken.None);
        }

在这种情况下,客户端ID和机密是应用程序类型已安装的应用程序。

在网络应用程序方面,如果还没有刷新令牌,那么我使用DotNetOpenAuth来触发身份验证,这里是代码片段:

   const string clientID = "someclientid";
    const string clientSecret = "somesecret";
    const string redirectUri = "http://localhost/Home/oauth2callback";
    AuthorizationServerDescription server = new AuthorizationServerDescription
    {
        AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
        TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
        ProtocolVersion = ProtocolVersion.V20
    };

    public ActionResult AuthenticateMe()
    {
        List<string> scope = new List<string> 
        { 
            GmailService.Scope.GmailCompose,
            GmailService.Scope.GmailReadonly,
            GmailService.Scope.GmailModify
        };

        WebServerClient consumer = new WebServerClient(server, clientID, clientSecret);

        // Here redirect to authorization site occurs
        OutgoingWebResponse response = consumer.PrepareRequestUserAuthorization(
            scope, new Uri(redirectUri));
        response.Headers["Location"] += "&access_type=offline&approval_prompt=force";
        return response.AsActionResult();

    }

    public void oauth2callback()
    {
        WebServerClient consumer = new WebServerClient(server, clientID, clientSecret);
        consumer.ClientCredentialApplicator =
            ClientCredentialApplicator.PostParameter(clientSecret);
        IAuthorizationState grantedAccess = consumer.ProcessUserAuthorization(null);

        string accessToken = grantedAccess.AccessToken;

    }

这是我想证实我怀疑的地方。

当存在RefreshToken时,我们使用以下代码段来调用Gmail API

    UserCredential uc = new UserCredential(flow, "someemail@gmail.com", new TokenResponse()
        {
            AccessToken = "lastaccesstoken",
            TokenType = "Bearer",
            RefreshToken = "supersecretrefreshtoken"
        });
    var refreshState = await uc.RefreshTokenAsync(CancellationToken.None);


    var svc = new GmailService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = uc,
        ApplicationName = "Gmail Test",
    });

这里我注意到的是,为了能够使用刷新令牌从桌面或Web端刷新,需要通过相同的客户端ID /秘密生成刷新令牌组合。我已经对它进行了测试,如果我们使用Installed应用程序作为桌面和网络的客户端ID的应用程序类型,它似乎没问题,但我想我的问题是,这些应用程序类型&#39 ;对于客户端ID,它们是否重要?

我这样做有什么不对吗?

提前致谢

0 个答案:

没有答案