![](/img/trans.png)
[英]How can I get Google and Facebook profile picture from logged in user in a controller with ASP.Net Core Identity 3.1?
[英]How can I get full facebook profile from ASP.NET MVC
我正在開發Web應用程序,在這里我需要獲取有關Facebook用戶的完整信息。 OAUTH2登錄成功完成。 但是之后,當我使用提供的訪問令牌調用graph.facebook.api
時,我graph.facebook.api
得到id,名稱和圖片。
問題是如何從Facebook獲取完整的用戶資料?
在Startup.Auth.cs中,我有以下代碼:
var facebookAuthOptions = new FacebookAuthenticationOptions()
{
AppId = "APP_ID_HERE",
AppSecret = "APP_SECRET_HERE",
Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:accesstoken"
, context.AccessToken
, ClaimValueTypes.String
, "Facebook"));
context.Identity.AddClaim(new Claim("urn:facebook:email"
, context.AccessToken
, ClaimValueTypes.Email
, "Facebook"));
context.Identity.AddClaim(new Claim("urn:facebook:about"
, context.AccessToken
, ClaimValueTypes.String
, "Facebook"));
return Task.FromResult(0);
}
}
};
app.UseFacebookAuthentication(facebookAuthOptions);
在我的控制器中,我像這樣調用facebook API:
var apiRequestUri = new Uri("https://graph.facebook.com/v2.8/me?fields=email,name,about,picture&access_token=" + accessToken);
var webClient = new System.Net.WebClient();
var json = webClient.DownloadString(apiRequestUri);
var profile = JsonConvert.DeserializeObject<FacebookProfile>(json);
但是我得到的只是身份證,姓名和圖片。 當我在圖形API控制台中嘗試類似的查詢時,我可以獲得所需的所有信息。 歡迎任何建議! 預先感謝您的幫助!
我已通過將要求添加到每個必填字段的facebookAuthOptions
來解決此問題。 用戶批准請求的信息后,我可以使用授權步驟中提供的令牌成功獲得來自Facebook API的響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.