2017-04-13 47 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; 
} 

我希望它可以幫助人在那裏。謝謝

相關問題