I have a Facebook desktop application and am using the Graph API.I am able to get the access token, but after that is done – I don”t know how to get the user”s ID.
Đang xem: How to add facebook login to php website
My flow is like this:
In my redirect page I get the code from Facebook.
Then I perform a HTTP request to graph.facebook.com/oauth/access_token with my API key and I get the access token in the response.
From that point on I can”t get the user ID.
Xem thêm: Share Code Bán Máy Tính Php Mysql ( Source Code Đồ Án 2020 )
How can this problem be solved?
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
If you want to use Graph API to get current user ID then just send a request to:
https://graph.facebook.com/me?access_token=…
The easiest way is
https://graph.facebook.com/me?fields=id&access_token=”xxxxx”then you will get json response which contains only userid.
The facebook acess token looks similar too”1249203702|2.h1MTNeLqcLqw__.86400.129394400-605430316|-WE1iH_CV-afTgyhDPc”
if you extract the middle part by using | to split you get
2.h1MTNeLqcLqw__.86400.129394400-605430316
then split again by –
the last part 605430316 is the user id.
Xem thêm: Tna Impact Wrestling Game Free Download {*Free}, Tna Wrestling Impact Mod Apk Download For Android
Here is the C# code to extract the user id from the access token:
public long ParseUserIdFromAccessToken(string accessToken) { Contract.Requires(!string.isNullOrEmpty(accessToken); /* * access_token: * 1249203702|2.h1MTNeLqcLqw__.86400.129394400-605430316|-WE1iH_CV-afTgyhDPc * |_______| * | * user id */ long userId = 0; var accessTokenParts = accessToken.Split(“|”); if (accessTokenParts.Length == 3) { var idPart = accessTokenParts<1>; if (!string.IsNullOrEmpty(idPart)) { var index = idPart.LastIndexOf(“-“); if (index >= 0) { string id = idPart.Substring(index + 1); if (!string.IsNullOrEmpty(id)) { return id; } } } } return null; }WARNING: The structure of the access token is undocumented and may not always fit the pattern above. Use it at your own risk.
UpdateDue to changes in Facebook.the preferred method to get userid from the encrypted access token is as follows:
try{ var fb = new FacebookClient(accessToken); var result = (IDictionary)fb.Get(“/me?fields=id”); return (string)result<"id">;}catch (FacebookOAuthException){ return null;}