I am using https://github.com/sanjeevdwivedi/facebook-csharp-sdk to integrate facebook in my wp8 app.
I want to know how to access the user email id using facebook-csharp-sdk below is the code I am using
FacebookSession session = FacebookSessionClient.LoginAsync("user_about_me,read_stream");
FacebookClient _fb = new FacebookClient(session.AccessToken);
dynamic parameters = new ExpandoObject();
parameters.access_token = session.AccessToken;
parameters.fields = "email,first_name,last_name";
dynamic result = await _fb.GetTaskAsync("me", parameters);
But I am getting only firstname , lastname and id of the logged in result field. Please suggest where am i missing?
You should ask for the email permission.
FacebookSession session = FacebookSessionClient.LoginAsync("user_about_me,read_stream,email");
The last item in the LoginAsync params I placed is email
See permissions for more info
Related
I have the following code to try connect the authenticator
var auth = new OAuth2Authenticator (
appId,
"email",
new Uri ("https://m.facebook.com/dialog/oauth/"),
new Uri ("http://www.facebook.com/connect/login_success.html"));
and when the dialog shows, I can see that it is requesting permission to see my email. But how do I get that email from the service?
I make the following call to get the Facebook ID
var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, args.Account);
request.GetResponseAsync ().ContinueWith (t => {
var obj = JsonValue.Parse (t.Result.GetResponseText ());
var id = obj ["id"];
});
But there is no email in the reponse that I get back. How can I get the email address of the user?
The lastest API version (2.4 at time of writing) uses Declarative Fields, so you need to explicitly request the email field like this:
https://graph.facebook.com/me?fields=email
I would like to fetch a user's email using the C# Facebook SDK. How can I do so? I've tried the code below, but I just get an empty email. Is it because I somehow need to ask for more rights? If so, how do I do that?
Facebook.FacebookClient fbc = new Facebook.FacebookClient(user.MobileServiceAuthenticationToken);
dynamic clientCredentials = await fbc.GetTaskAsync("oauth/access_token",
new{client_id = facebookClientId,client_secret = facebookClientSecret,
grant_type = "client_credentials",redirect_uri = "https://xxx.azure-mobile.net/signin-facebook"});
fbc.AccessToken = clientCredentials.access_token;
fbc.AppId = facebookClientId;
fbc.AppSecret = facebookClientSecret;
string id = user.UserId.Replace("Facebook:", string.Empty);
dynamic result = await fbc.GetTaskAsync(id + "?fields=id,name,picture,last_name,first_name,gender");
Best regards
TJ78
You need to gather the email permission in the login Url's scope parameter, otherwise you will not be able to receive the email field.
I have a Grails project (v2.4.2) that is making use of the spring-security-facebook:0.17 plugin to authenticate via Spring Security. At first sight, all seems well. However, there is a large set of users that for some unknown reason I cannot access their email address. I am using spring social to grab the email. I have permission and it is set in the scope. Here is a code snippet where I authenticate a new user:
log.info("Create domain for facebook user $token.uid")
//Use Spring Social Facebook to load details for current user from Facebook API
log.info("create: FacebookAuthToken: $token")
log.info("created FacebookAuthToken.FacebookAccessToken = ${token.accessToken}")
Facebook facebook = new FacebookTemplate(token.accessToken.accessToken)
org.springframework.social.facebook.api.User fbProfile = facebook.userOperations().getUserProfile()
// Check if email is actual granted because in production some are coming back null
boolean isEmailGranted=false
List<Permission> permissions = facebook?.userOperations()?.getUserPermissions()
String permissionString = "["
for (int i=0;i<permissions.size();i++) {
permissionString += "["+ permissions[i].getName() + ":" + permissions[i].getStatus()+"]"
if (permissions[i].getName()=="email" && permissions[i].isGranted())
isEmailGranted=true
}
permissionString += "]"
log.info("create: Facebook Permissions = " + permissionString)
def grailsWebRequest = WebUtils.retrieveGrailsWebRequest()
def flash = grailsWebRequest.flashScope
if (!isEmailGranted) {
log.warn("create: Unable to subscribe facebook user because email priviledge was not granted.")
flash.message = 'Login to Facebook failed. We must have access to your email address in order to proceed with login.'
throw new InsufficientAuthenticationException("Facebook email not accessible")
}
log.info("created: ")
String email = fbProfile.getEmail()
String firstName = fbProfile.getFirstName()
String lastName = fbProfile.getLastName()
String fullName = fbProfile.getName()
String username = firstName
String password = token.accessToken.accessToken
if (!email) {
log.error("create: Permission was granted to use facebook email but the value is null.")
flash.message = 'Login to Facebook failed. We are temporarily unable to access your email although permission has been granted'
throw new InsufficientAuthenticationException("Facebook email not accessible for unknown reason")
}
Why would I receive an empty email when permission has been granted? Is there a preferred method for handling this behavior (other than failing the authentication and making up a fake email address). Many thanks!
The documentation for the 'email' field of the 'user' object ( https://developers.facebook.com/docs/reference/api/user/ ) clarifies the expected behaviour here, which is:
"this field will not be returned if no valid email address is available"
There is a detailed explanation about different situations where an email won't be sent. Please check it out:
https://developers.facebook.com/bugs/298946933534016/
I hope it helps.
i want to get the facebook friend's list from my application, but returns me data: []... empty! :(
the scenario it's this:
C#
const string QueryToGetFbInfo = "me";
const string QueryToGetFbPhoto = "me?fields=picture.width(200).height(200)";
const string QueryToGetFriendList = "me/friends?fields=name,picture.width(100).height(100)";
private void btnFacebook_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
FacebookSessionClient fbSession = new FacebookSessionClient("FB_APP_ID");
fbSession.LoginWithApp("public_profile, user_friends, read_friendlists, email", "custom_state_string");
}
private async void tbnShowfriends_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
FacebookSession session = SessionStorage.Load();
FacebookClient client = new FacebookClient(session.AccessToken);
// Get Facebook FriendList
dynamic friends = await client.GetTaskAsync(QueryToGetFriendList);
//Get Facebook user info
dynamic result = await client.GetTaskAsync(QueryToGetFbInfo);
GraphUser user = new GraphUser(result);
//Get profile picture from facebook
dynamic result1 = await client.GetTaskAsync(QueryToGetFbPhoto);
JsonObject json = result1.picture.data;
var picture = (IDictionary<string, object>)json;
....
}
Facebook App Config has the permissions... forgot some ?.. the new panel confuses me a little, Maybe I forgot something in the application settings...
when i select Api Graph, returns me, a friendlist, fine!
but when I select my application, I do not return anything
What I forgot ?... how can i fix this problem ?
You authed Graph Explorer using API v1.0 which means that that app will get all your friends until 4/30/2014. You app you authed using v2.0 or v2.1 which means that you will only get friends that are also using the app
So I tried to post on my friends wall using the following code:
var fb = new FacebookClient(_accessToken);
dynamic parameters = new ExpandoObject();
parameters.message = "Google is your friend";
parameters.link = "http://gidf.de/";
parameters.Name = "Test";
parameters.from = new { id = "100000", name = "me" };
parameters.to = new { id = "1000001", name = "friend" };
dynamic result = fb.Post("1000001/feed", parameters);
However, I get told that my application does not support this. I did some googling work, and read that [USER_ID]/feed is deprecated and that I have to invoke the feed dialog to ask the user to publish it. How would I go on doing this with the C# SDK?
As of February 6, 2013, you can't post to Friends Timeline on behalf of the user.
Read Here: https://developers.facebook.com/roadmap/completed-changes/
Client-Side you can use the FB.ui method to pop up the feed dialog.
Here's an example: https://stackoverflow.com/a/15426243/1405120
Server-Side, you can use the URL Redirection.
https://www.facebook.com/dialog/feed?
app_id=458358780877780
&link=https://developers.facebook.com/docs/reference/dialogs/
&redirect_uri=https://mighty-lowlands-6381.herokuapp.com/
Read more about Feed dialog here, https://developers.facebook.com/docs/reference/dialogs/feed/