2017-04-13 49 views
1

我是OAuth验证的新手,因为当我尝试访问Google的OAuth屏幕时,从未向我提供访问代码。我可以访问登录屏幕,然后如果我选择允许或拒绝,它会给我一个成功消息。没有错误,但有时会给出无效协议的例外,但我不知道这是为什么。适用于UWP的Google OAuth v2

我想从herehere获得帮助,但没有任何工作。

,我使用的代码如下:

 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([email protected]"{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,它不包含Google示例中所述的任何可视为code的内容。 那么有人可以指出我在这里做错了吗?这应该很简单,但我不知道我做错了什么。谢谢

+0

我已使用官方[代码示例](https://github.com/googlesamples/oauth-apps-for-windows)测试了Google OAuth v2的UWP。我使用'WebAuthenticationBroker.AuthenticateAsync'来获得Web认证结果。但是我无法重现您的问题,可以提供您的源代码,以便我可以针对此问题进行更多测试? –

+0

嗯,我想这只是'endura'末尾'''的一个问题,我把它放在这里,但是我没有在我的实际代码中使用它,并且我以某种方式对它进行了整理。谢谢您的好意。 – Ahmar

回答

0

我不确定上次如何设法解决问题,因为我的评论建议。 UWP中的WebAuthenticationBroker不再以这种方式工作,或者至少我认为是这样。它实际上要求startURLcallbackURL

我碰到了同样的问题,在我的问题,昨天我解决它通过把我的应用程序的redirectUricallbackURLWebAuthenticationBroker的地方已经不抛出异常,它提供了一个成功的结果。

所以要解决这个问题,这是你会怎么做:

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

我希望它可以帮助人在那里。谢谢

相关问题