2017-04-13 26 views
1

私はOAuth検証の初心者です。ここでは、GoogleのOAuth画面にアクセスしようとするとアクセスコードが提供されないため、ここでオプションを使い果たしました。私はログイン画面にアクセスして、許可または拒否を選択すると成功メッセージを表示します。エラーはありませんが、無効なプロトコルの例外が表示されることがありますが、理由はわかりません。Google OAuth v2 for UWP

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([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 for UWPをテストしました。私は 'WebAuthenticationBroker.AuthenticateAsync'を使ってWeb認証結果を取得しました。しかし、私はあなたの問題を再現することはできません、私はこの問題でより多くのテストを行うことができます私のソースコードを提供することができますか? –

+0

まあ、 'endURL'の最後に'? 'がついているだけの問題だったと思いますが、ここに入れましたが実際のコードでは使わなかったので何とか並べ替えました。しかし、ありがとう。 – Ahmar

答えて

0

私のコメントが示唆したように私が最後にこの問題をどのように解決したかについてはわかりません。 UWPのWebAuthenticationBrokerはそれ以上は機能しません。少なくとも私はそう考えています。実際にはstartURLcallbackURLを求めています。

私は昨日、私の質問のように、同じ問題に出くわしたと私はもう例外をスローし、それが成功した結果を提供していませんcallbackURLWebAuthenticationBrokerの代わりに私のアプリのredirectUriを入れて、それを解決しました。

だから、問題を解決するために、これはあなたがそれを行うだろうかです:

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

私はそれがそこに誰かに役立ちます願っています。ありがとう