I'm writing a simple application in Xamarin.Forms that allows users the authentication through socials such as Facebook, Google, Instagram, Twitter, etc. I'm using Xamarin.auth to do this. I have a problem with Facebook Login. I used the same code reported in the official guide:
https://components.xamarin.com/gettingstarted/xamarin.auth
But after the application has requested the user for credentials, the response (in the code, t.Result.GetResponseText(), section 3) is a json contains the Facebook user name and surname, and a field called "id". Instead, I need all profile informations of the user, such as age, gender, etc. I suppose that I have to use the id returned for build an http request to a facebook service for retrieve data from the id.
You need to specify in your facebook link the requiered fields.
Example:
var request = new OAuth2Request("GET", new Uri("https://graph.facebook.com/me?fields=email,first_name,last_name,gender,picture"), null, eventArgs.Account);
After how I Get the fields
var response = await request.GetResponseAsync();
var obj = JObject.Parse(response.GetResponseText());
var id = obj["id"].ToString().Replace("\"", "");
var name = obj["first_name"].ToString().Replace("\"", "");
var lastName = obj["last_name"].ToString().Replace("\"", "");
var email = obj["email"].ToString().Replace("\"", "");
Related
I have already implemented a code for accessing logged facebook user via our website for that i have created AppID and appSecret Key and it will return all data(eg.email,likes,birthday,friends).
but the problem is if logged user is only having a page then my Graph API string will not fetch result properly it only contains ID nothing else.
My code to get user details.
OAuthWebSecurity.RegisterClient(new FacebookExtendedClient(
"655618811231635",
"a05a82145a29f38a117f66ba56345081",
"email,likes",
new Func<string, object, string>(fieldsTransformer),
"email"));
and this is for getting me result.
FacebookClient facebookClient = new FacebookClient(accessToken);
facebookClient.AppId = "655618811231635";
facebookClient.AppSecret = "a05a82145a29f38a117f66ba56345081";
var getResult = facebookClient.Get<IDictionary<string, object>>("me", new { fields = this.fields });
if logged user is simple user(which having friends,groups etc) then it will work like charm but if it is user which having a Page in it then it will not display any result.
I am using OAuthWebSecurity to make my MVC application Facebook login ready. Everything works as expected.
However, the provider only return the Facebook username (email address) back to my client. I like to also receive the Firstname and the Lastname but did not find any example how to do this with OAuthWebSecurity.
Is it possible to extend the Facebook provider without make use of the big Facebook sdk?
Ok, after some more research I find out that it is the easiest way to just use the Facebook SDK and use following code fregment to access this information:
var client = new FacebookClient(result.ExtraData["accesstoken"]);
dynamic me = client.Get("me");
string firstName = me.first_name;
string lastName = me.last_name;
string email = me.email;
string gender = me.gender;
It would be much more complicated to extend the Facebook OAuth provider.
I'm currently building a website where I'll offer the users a link to share a photo on Facebook, whereby the application would:
Provide the user with a link to request permissions from Facebook
Redirect back to the website with the access code from the user's acceptance of the permissions
Server-side post the photo to the user's photos
To generate the link for the first step, I'm doing this:
var fb = new FacebookClient();
var options = new
{
client_id = "MY_APP_ID",
client_secret = "MY_APP_SECRET",
redirect_uri = string.Format("http://localhost:51182/Home/FacebookShare?path={0}", Server.UrlEncode(path)),
response_type = "code",
scope = "publish_stream"
};
var loginUrl = fb.GetLoginUrl(options);
Then this loginUrl value is added to my MVC ViewModel and used in a link in the View.
However, I've noticed that the loginUrl contains the client_secret value in clear text. Isn't this a bad thing? Shouldn't users not be able to see the client_secret? Did I go about this the wrong way?
The fb.GetLoginUrl method is rather simple, in that it just adds anything you give it inside the options object as parameters to the URL created.
So take
client_secret = "MY_APP_SECRET",
out of your options object, and it should not show up in the login URL any more.
I have a minor problem in facebook c# sdk
I just want to get the facebook user id without asking for permissions , just from the signed request. Up to now i have written the code below:
var current = new DefaultFacebookApplication { AppId ="***", AppSecret = "***" };
dynamic signedRequest = FacebookSignedRequest.Parse(current, Request);
var UserId = (string)signedRequest.Data.user.id;
My first question is :is that is possible? Is it possible to get the user id without oauth?
Secondly , if it is possible is the (string)signedRequest.Data.user.id statement correct?;
You cannot get the Facebook user id from the signed_request unless they've authenticated your app. It is a privacy issue.
I'm trying to convert friend pages into fan pages (most businesses have created friend pages by mistake) and am trying to email everyone on a friend list about the move.
I've tried
FQL "SELECT email FROM user WHERE uid=xxxx"
Creating groups (not good for 5000 friend pages)
Restfb: Connection myFriends = facebookClient.fetchConnection("me/friends", User.class) etc;
The FQL and RestFB methods are both returning nada, group email method is just plain messy.
Is this a security feature or can this data ever by returned for my purpose?
Cheers
to access user's private informations like email,posts etc you should have the permission to access those details .for more details visit https://developers.facebook.com/docs/reference/api/permissions/
If you are using restfb (Facebook graph API and old REST API client wriiten in Java), and you want to access all friend list after Oauth authentication, you can use following method to access the friends list and facebook id's,
public List<ArrayList> findFacebookFriendsUsingRest(String facebookAccessToken){
List<ArrayList> myFacebookFriendList= new ArrayList();
final FacebookClient facebookClient;
facebookClient = new DefaultFacebookClient(facebookAccessToken);
User user = facebookClient.fetchObject("me", User.class);
String userName = user.getFirstName();
if (userName == null){
userName = user.getLastName();
}
String userEmail = user.getEmail();
com.restfb.Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
System.out.println("Count of my friends: " + myFriends.getData().size());
for(User friend: myFriends.getData()){
System.out.println("Friends id and name: "+friend.getId()+" , "+friend.getName());
myFacebookFriendList.add(friend.getName());
}
System.out.println("All Friends : "+myFacebookFriendList);
}
Facebook does not provide user, unless the user authorizes your application to access it. It's on account of security and user privacy, imagine how terrible it would be if everybody could have at her disposal all FB user's email addresses.
Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class ,Parameter.with("fields", "id, name, picture, email"));
You can specify any of the properties listed under
http://developers.facebook.com/docs/reference/api/user
The only thing is that FB does not seam to return the friends email..
I'm using restFb 1.5.3