2017-11-10 14 views
0

WebAuthenticationBrokerを使用してGoogle認証を使用するアプリケーションを作成しようとしています。私はGoogleからのユーザー情報を取得するには、次の方法を使用したコードを取得した後、UWPのGoogle認証エラー

public void SignInUserAsync() 
{ 
    var code = await AuthenticateUsingWebAuthenticationBroker(); 
    if (string.IsNullOrEmpty(code)) 
     return; 
    var account = await ConvertCodeToAccount(code); 
} 

private async Task<string> AuthenticateUsingWebAuthenticationBroker() 
{ 
    var googleUrl = Common.Constants.AuthorizeUrl + "?client_id=" + Common.Constants.GoogleClientId; 
    googleUrl += "&redirect_uri=" + Common.Constants.GoogleCallbackUrl; 
    googleUrl += "&response_type=code"; 
    googleUrl += "&scope=" + Common.Constants.Scope; 

    var startUri = new Uri(googleUrl); 

    string result = string.Empty; 

    try 
    { 
     var webAuthenticationResult = await 
       WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, startUri, 
        new Uri(Common.Constants.GoogleCallbackUrl)); 

      switch (webAuthenticationResult.ResponseStatus) 
      { 
       case WebAuthenticationStatus.Success: 
        // Successful authentication 
        result = webAuthenticationResult.ResponseData.Substring(webAuthenticationResult.ResponseData.IndexOf('=') + 1); 
        break; 
       case WebAuthenticationStatus.UserCancel: 
        break; 
       case WebAuthenticationStatus.ErrorHttp: 
        // HTTP Error 
        result = webAuthenticationResult.ResponseErrorDetail.ToString(); 
        result = string.Empty; 
        break; 
       default: 
        break; 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex); 
     } 

     return result; 
    } 

をGoogleからのコードを取得するために、次のコードを書かれています。

private async Task<Account> ConvertCodeToAccount(string code) 
    { 
     var httpClient = new HttpClient(); 

     IHttpContent content = new HttpFormUrlEncodedContent(new Dictionary<string, string> 
     { 
      {"code", code }, 
      {"client_id", Common.Constants.GoogleClientId }, 
      {"client_secret", Common.Constants.GoogleClientSecret }, 
      {"redirect_uri", Common.Constants.GoogleCallbackUrl }, 
      {"grant_type", "authorization_code" } 
     }); 

     var accessTokenResponse = await httpClient.PostAsync(new Uri(Common.Constants.AccessTokenUrl), content); 
     var responseDict = 
      JsonConvert.DeserializeObject<Dictionary<string, string>>(accessTokenResponse.Content.ToString()); 

     return new Account(null, responseDict); 
    } 


public static class Constants 
{ 
    public static string Scope = "https://www.googleapis.com/auth/userinfo.email"; 
    public static string AuthorizeUrl = "https://accounts.google.com/o/oauth2/auth"; 
    public static string AccessTokenUrl = "https://www.googleapis.com/oauth2/v4/token"; 
    public static string UserInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo"; 

    public const string GoogleCallbackUrl = "<My-Callback-Url>"; 
    public const string GoogleClientId = "<My-Client-Id>"; 
    public const string GoogleClientSecret = "<My-Client-Secret>"; 
    public const string UwpAccessTokenUrl = "https://accounts.google.com/o/oauth2/token"; 
    public const string UwpScope = "email"; 
} 

しかし、私は、Windows 10のデスクトップアプリケーション上でのユーザーのデータを取得しようとすると、それはWindowsの携帯電話10それが正常に動作し、私に適切なデータを返すには、エラー Windows 10

次私を与えます。それを修正する方法を教えてください。ありがとう。

答えて

0

最初に気付くのは、トークン交換の要求コンテンツタイプが不足していることです。

これが動作するかどうかを参照してください:

IHttpContent content = new HttpFormUrlEncodedContent(new Dictionary<string, string> 
{ 
    {"code", code }, 
    {"client_id", Common.Constants.GoogleClientId }, 
    {"client_secret", Common.Constants.GoogleClientSecret }, 
    {"redirect_uri", Common.Constants.GoogleCallbackUrl }, 
    {"grant_type", "authorization_code" } 
}); 

// add this! 
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); 

var accessTokenResponse = await httpClient.PostAsync(new Uri(Common.Constants.AccessTokenUrl), content); 
+0

はまだこれで私を助けてください –

+0

働いていません。 –

関連する問題