2017-10-10 96 views
1

我有一個C#MVC項目。我試圖通過從Google獲取用戶信息來幫助用戶註冊過程。我想訪問名字,姓氏,電子郵件和手機號碼。所有都是必填字段。我相信我需要使用Google People API。 Google+ API工作正常,但沒有手機號碼。我不確定如何獲取這些數據。目前在startup.auth我:MVC外部登錄 - 使用Google People API

 var googleOptions = new GoogleOAuth2AuthenticationOptions() 
     { 
      ClientId = ConfigurationManager.AppSettings["GoogleClientId"], 
      ClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"], 
      Provider = new GoogleOAuth2AuthenticationProvider 
      { 
       OnAuthenticated = context => 
       { 
        context.Identity.AddClaim(new Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google")); 
        context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email)); 
        context.Identity.AddClaim(new Claim(ClaimTypes.Uri, context.User["image"]["url"].ToString())); 
        return Task.FromResult(true); 
       } 
      } 
     }; 
     googleOptions.Scope.Add("https://www.googleapis.com/auth/user.phonenumbers.read"); 
     googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email"); 
     googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.profile"); 
     app.UseGoogleAuthentication(googleOptions); 

而在我的控制,我有:

 if (loginInfo.Login.LoginProvider == "Google") 
     { 
      if (loginInfo.Email == null) 
       loginInfo.Email = GetSchemasClaimValue(loginInfo, "emailaddress"); 
      firstName = GetSchemasClaimValue(loginInfo, "givenname"); 
      lastName = GetSchemasClaimValue(loginInfo, "surname"); 
      mobilePhone = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.MobilePhone); 


      //proPic = GetSchemasClaimValue(loginInfo, "uri"); 
     } 

所有信息,除了根據需要手機訪問和工作。我只是不確定如何檢索這些數據。我希望它會在loginInfo中顯示爲Claim,但對於這種情況不存在任何索賠。系統會提示用戶授予應用程序訪問手機的權限,所以我有點困惑,爲什麼沒有聲明。請求是否需要添加到我的startup.auth中?這將如何工作?任何幫助,將不勝感激。

回答

0

我已經完成了類似的功能,即從Google People API獲取數據,my sample Xamarin project後端託管在Azure移動應用程序(aka App Services)上。我做了如下。

using (HttpClient client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Authorization = 
     AuthenticationHeaderValue.Parse("Bearer " + accessToken); 

    using (HttpResponseMessage response = await client.GetAsync("https://www.googleapis.com/plus/v1/people/me")) 
    { 
     var googlePlusUserInfo = 
      JsonConvert.DeserializeObject<GooglePlusUserInfo>(await response.Content.ReadAsStringAsync()); 

     googlePlusUserInfo.Email = googlePlusUserInfo.Emails.Count() > 0 ? 

     googlePlusUserInfo.Emails.First().EmailAddress : ""; 

     googlePlusUserInfo.ProfilePicure.ImageUrl = 
     googlePlusUserInfo.ProfilePicure.ImageUrl.Split(new char[] { '?' })[0]; 

     return googlePlusUserInfo; 
    } 
} 

Complete code on Github

如果需要的電話號碼進行檢索,然後我們就可以用得到它:

googlePlusUserInfo.PhoneNumbers.CanonicalForm 

當然,the model GooglePlusUserInfo需要被更新,以符合JSON結構返回第一。返回的電話號碼的谷歌用戶配置文件的JSON是

{ 
    "resourceName": "people/...", 
    "etag": "...", 
    "phoneNumbers": [ 
    { 
     "metadata": { 
     "primary": true, 
     "source": { 
      "type": "CONTACT", 
      "id": "1" 
     } 
     }, 
     "value": "88880000", 
     "canonicalForm": "+65888800000", 
     "type": "mobile", 
     "formattedType": "Mobile" 
    } 
    ] 
} 

希望這可以幫助,請糾正我,如果我錯了。

+0

您好,我已經嘗試這種方法,但是我的JSON結果是這樣的: {{ 「種」: 「加#人」, 「ETAG」: 「\」 Sh4n9u6xxxNdphy12JkxmY \ 「」, 「電子郵件」:[{ 「值」: 「[email protected]」, 「類型」: 「賬號」 } ], 「的objectType」: 「人」 , 「ID」: 「114157772521111131101」, 「顯示名」: 「尼克車」, 「名」:{ 「familyName」: 「切」, 「給定名稱」: 「尼克」 }, 「圖像」:{ 「URL」: 「https://開頭XXX = 50」, 「ISDEFAULT」:假 }, 「isPlusUser」:假, 「語言」: 「EN」, 「verified」:false, 「domain」:「xxx」 }} 我找不到電話號碼 –

+0

此答案是從[Google+ API](https://developers.google。com/+/web/api/rest/latest/people),這與[Google People API](https://developers.google.com/people/api/rest/)不同。 –

+0

@NicolasChetty請參閱https://developers.google.com/people/api/rest/v1/people/get。看起來你需要在'personFields'中指定「phoneNumbers」,字段掩碼用於限制返回該人的哪些字段。 – ChunLin