2016-09-15 111 views
3

我需要从谷歌登录接收用户姓氏。 这是我的代码:谷歌登录不给我GIVEN_NAME和FAMILY_NAME

internal static string GoogleAppId = "XXXXXXXXXXXXX-scvpbtaoijuciinu1oneu7ijqtdvcpce.apps.googleusercontent.com"; 
    internal static string GoogleAppSecret = "XXXXXXXXXXXUJuZNHhf6-8"; 
    internal static Uri GoogleStartUri = new Uri("https://accounts.google.com/o/oauth2/auth?client_id=" + Uri.EscapeDataString(GoogleAppId) + "&redirect_uri=" + Uri.EscapeDataString("urn:ietf:wg:oauth:2.0:oob") + "&response_type=code&scope=" + Uri.EscapeDataString("profile openid https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me email")); 
    private string GoogleCallbackUrl = "urn:ietf:wg:oauth:2.0:oob"; 


    internal static Uri GoogleEndUri = new Uri("https://accounts.google.com/o/oauth2/approval?"); 

    public static void AuthenticateAndContinue() 
    { 
     StringBuilder googleUrl = new StringBuilder(); 
     googleUrl.Append("https://accounts.google.com/o/oauth2/auth?client_id="); 
     googleUrl.Append(Uri.EscapeDataString(GoogleAppId)); 
     googleUrl.Append("&scope=openid%20email%20profile"); 
     googleUrl.Append("&redirect_uri="); 
     googleUrl.Append(Uri.EscapeDataString(GoogleCallbackUrl)); 
     googleUrl.Append("&response_type=code"); 

     Uri startUri = new Uri(googleUrl.ToString()); 

     WebAuthenticationBroker.AuthenticateAndContinue(startUri, GoogleEndUri, null, WebAuthenticationOptions.UseTitle); 
    } 

    public static async Task<string> GetToken(string code) 
    { 
     var client = new HttpClient(); 
     var auth = await client.PostAsync("https://accounts.google.com/o/oauth2/token", new FormUrlEncodedContent(new[] 
     { 
      new KeyValuePair<string, string>("code", code), 
      new KeyValuePair<string, string>("client_id",GoogleAppId), 
      new KeyValuePair<string, string>("client_secret",GoogleAppSecret), 
      new KeyValuePair<string, string>("grant_type","authorization_code"), 
      new KeyValuePair<string, string>("redirect_uri","urn:ietf:wg:oauth:2.0:oob"), 
      //new KeyValuePair<string, string>("redirect_uri","http://localhost") 
     })); 

     var data = await auth.Content.ReadAsStringAsync(); 
     var response = JsonConvert.DeserializeObject<TokenResponse>(data); 
     System.Diagnostics.Debug.WriteLine(response.id_token); 
     return response.id_token; 
    } 

    internal async void Continue(IContinuationActivatedEventArgs args) 
        { 
            if (args.Kind == ActivationKind.WebAuthenticationBrokerContinuation) 
            { 
                var data = ((WebAuthenticationBrokerContinuationEventArgs)args).WebAuthenticationResult.ResponseData.Split('&')[0]; 
                var code = data.Split('=')[1]; 
                System.Diagnostics.Debug.WriteLine($"code: {code}"); 
                var token = await GoogleManager.GetToken(code); 

            } 
        } 

后来我把这里的令牌:

https://www.googleapis.com/oauth2/v3/tokeninfo?id_token= {0}

我收到一个JSON但FAMILY_NAMEGIVEN_NAME失踪:

{ 
"iss": "accounts.google.com", 
"at_hash": "9Nxxxxxxxxxxxxxxx9uBNHA", 
"aud": "99655070360-scvpbtaoijuXXXXXXXXXXXXXXXXvcpce.apps.googleusercontent.com", 
"sub": "106XXXXXXXXXXXX86619603", 
"email_verified": "true", 
"azp": "996550XXXXXXXXXXXXXXXijqtdvcpce.apps.googleusercontent.com", 
"email": "[email protected]", 
"iat": "1473XXXXXXXXXX", 
"exp": "14738665XXX", 
"alg": "RS256", 
"kid": "96bffd2afXXXXXXXXXXXXXXXXXXXXXXXX" 
} 

我在做什么错?

回答

1

您正在使用的端点用于获取有关令牌的信息。您应该使用:

https://www.googleapis.com/oauth2/v3/userinfo?access_token={0}