i want to fetch user details with profile picture using Facebook c# SDK.I have done some coding and able to login using Facebook in my app.I am explaining my code below.
immediateHelp.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Params["code"] != null)
{
//Facebook.FacebookAPI api = new Facebook.FacebookAPI();
FacebookClient client = new Facebook.FacebookClient(GetAccessToken());
object me = client.Get("/me");
lblTextImage.ImageUrl="need picture here" ;
lblTextEmail.Text="need email here";
// JSONObject meFriends = client.Get("/me/friends");
}
}
}
private string GetAccessToken()
{
if (HttpRuntime.Cache["access_token"] == null)
{
Dictionary<string, string> args = GetOauthTokens(Request.Params["code"]);
HttpRuntime.Cache.Insert("access_token", args["access_token"], null, DateTime.Now.AddMinutes(Convert.ToDouble(args["expires"])), TimeSpan.Zero);
}
return HttpRuntime.Cache["access_token"].ToString();
}
private Dictionary<string, string> GetOauthTokens(string code)
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string clientId = "*************";
string redirectUrl = "http://localhost:3440/immediateHelp.aspx";
string clientSecret = "************************";
string scope = "user_photos,email";
string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&scope={4}",
clientId, redirectUrl, clientSecret, code, scope);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string retVal = reader.ReadToEnd();
foreach (string token in retVal.Split('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
return tokens;
}
I need to attach image in image field and email in text Box after login.I need also logout properties with destroy session using Facebook c# SDK.Please help me to resolve this.
The following code will help you get email and profile picture. The code client.Get("/me?fields=picture")) return picture in format of Json so that we need to extract the url as the code show :
FacebookClient client = new FacebookClient(access_token);
dynamic myInfo = fb.Get("/me?fields=email");
var picture = (((JsonObject)(JsonObject)client.Get("/me?fields=picture"))["picture"]);
var url = (string)(((JsonObject)(((JsonObject)picture)["data"]))["url"]);
lblTextImage.ImageUrl=url;
lblTextEmail.Text=myInfo.email;
Hop this work for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.