c# - 适用于 UWP 的 Google OAuth v2

标签 c# uwp google-oauth

我是 OAuth 验证的新手,我已经用完了这里的选项,因为当我尝试访问 Google 的 OAuth 屏幕时,它从未向我提供访问代码。我可以访问登录屏幕,然后如果我选择允许或拒绝,它会给我一条成功消息。没有错误,但有时它会给出无效协议(protocol)的异常,但我不知道为什么会这样。

我正在尝试从 here 获得帮助还有来自 here , 但没有任何效果。

我使用的代码如下:

        string state = GenerateRandomBase64Url(32);
        string code_verifier = GenerateRandomBase64Url(32);
        string code_challenge = GenerateBase64urlencodeNoPadding(sha256(code_verifier));

        string clientID = "1037832553054-2ktd0l6dop546i1ti312r2doi63sglfe.apps.googleusercontent.com";
        string redirectURI = "uwp.app:/oauth2redirect";
        string authorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth";
        string tokenEndpoint = "https://www.googleapis.com/oauth2/v4/token";
        string userInfoEndpoint = "https://www.googleapis.com/oauth2/v3/userinfo";
        string youtubeScope = "https://www.googleapis.com/auth/youtube";
        string code_challenge_method = "S256";

        ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
        localSettings.Values["state"] = state;
        localSettings.Values["code_verifier"] = code_verifier;

        string authorizationRequest = string.Format($@"{authorizationEndpoint}?response_type=code&scope={Uri.EscapeDataString(youtubeScope)}&redirect_uri={Uri.EscapeDataString(redirectURI)}&client_id={clientID}&state={state}&code_challenge={code_challenge}&code_challenge_method={code_challenge_method}&login_hint={EmailBox.Text.Trim()}");

        string endURL = "https://accounts.google.com/o/oauth2/approval?";
        // I don't know if this is actually valid because google says that this Url is not available
        Uri startURI = new Uri(authorizationRequest);
        Uri endURI = new Uri(endURL);
        string result = string.Empty;
        try
        {
            //var success = Windows.System.Launcher.LaunchUriAsync(new Uri(authorizationRequest));

            WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startURI, endURI);
            switch (webAuthenticationResult.ResponseStatus)
            {
                // Successful authentication.  
                case WebAuthenticationStatus.Success:
                    result = webAuthenticationResult.ResponseData.ToString();
                    break;
                // HTTP error.  
                case WebAuthenticationStatus.ErrorHttp:
                    result = webAuthenticationResult.ResponseErrorDetail.ToString();
                    break;
                default:
                    result = webAuthenticationResult.ResponseData.ToString();
                    break;
            }

        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
        response.Text = result;

 // the string that I get looks like this
 // https://accounts.google.com/o/oauth2/approval?as=410b4db829b95fce&pageId=none&xsrfsign=AMt42IIAAAAAWO9e-l2loPR2RJ4_HzjfNiGJbiESOyoh

我不知道这是否是在 UWP 应用程序中执行此操作的正确方法。此外,我从结果中获得的字符串只是一个 Url,它不包含任何将被视为 code 的内容,如 Google 示例中所述。 那么有人可以指出我在这里做错了什么吗?这应该很简单,但我不知道我做错了什么。谢谢

最佳答案

我不确定上次我是如何按照我的评论建议解决问题的。 UWP 中的 WebAuthenticationBroker 不再以这种方式工作,或者至少我认为是这样。它实际上要求一个 startURL 和一个 callbackURL

我昨天遇到了与我的问题相同的问题,我通过将应用程序的 redirectUri 替换为 callbackURLWebAuthenticationBroker< 解决了这个问题 不再抛出异常,它提供了一个成功的结果。

所以要解决这个问题,你会这样做:

string redirectURI = "uwp.app:/oauth2redirect";

Uri startURI = new Uri(authorizationRequest); // as in the question
Uri endURI = new Uri(redirectUri); // change to 'redirectUri'
WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startURI, endURI);
switch(webAuthenticationResult.ResponseStatus)
{
      case WebAuthenticationStatus.Success:
      break;
}

希望对大家有所帮助。谢谢

关于c# - 适用于 UWP 的 Google OAuth v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43391513/

相关文章:

c# - IDataErrorInfo 调用绑定(bind)对象而不是 DataContext

c# - UWP RichEditBox : How to enable HyperLinks for loaded RTF files (Document. LoadFromStream)

c# - 透明 UWP 窗口 10

java - 解码java google oauth2.0 JWT

c# - 如何使用刷新 token 更新访问 token ?

javascript - 使用 Passport-google-oauth 向 req.user 添加附加数据

c# - 求请求时传入回调url

c# - 使用 C# 自动设置 Internet Explorer/Windows 以使用 Socks5 代理

c# - 如何从 ASP.NET5 中的 config.json 正确读取嵌套的配置值?

c# - 多个框架依赖匹配 [UWP]