Facebook C# SDK Get Current User - c#

I m working on a Facebook App. Not a website.
I am trying to use Facebook C# SDK and trying to get Current User and Query Current User info.
How do i do that?
Also, When i try to use an app it s asking for Adding the app, requesting permission to access data, how do i do that also?
Is there a comprehensive examples of these things?

If you're stuck working with .NET 3.5, this will work:
var facebookClient = new FacebookClient(FacebookAccessToken);
var me = facebookClient.Get("me") as JsonObject;
var uid = me["id"];

You need the access token. Once you get the access token make a request to me
var fb = new FacebookClient("access_token");
dynamic result = fb.Get("me", new [] { fields = "id" });
var userId = result.id;

Related

Using OAuth2 eBay authentication with .NET SDK

I have been searching throughout the internet for a solution to use OAuth2 user token with eBay's .NET SDK and could find ANY SINGLE solution. The code I have is like following:
var ctx = new ApiContext();
ctx.Version = "897";
ctx.ApiCredential.ApiAccount.Application = "//something here";
ctx.ApiCredential.ApiAccount.Developer = "//something here";
ctx.ApiCredential.ApiAccount.Certificate = "//something here";
ctx.ApiCredential.eBayToken = "v^1.1... // this is OAuth2 token";
var getUser = new GetUserCall();
getUser.OutputSelector = new string[] { "UserID","Site" };
getUser.GetUser();
What I find really irritating is the fact that there is a possibility to use the new OAuth2 token with trading API, and I know this for a fact, because if you add an HTTP header to any Trading API call like:
X-EBAY-API-IAF-TOKEN:q2eq2eq2eq2q2eq2e
It will work, but I haven't found anywhere in the documentation to pass the IAF-TOKEN (OAuth2) token via .NET SDK calls...
Has anyone else been trying this? Is there nay way to pass the OAuth2 token via .NET SDK and then fetch the results ?
Because if I try to pass the OAuth2 token like this:
ctx.ApiCredential.eBayToken = "v^1.1... // this is OAuth2 token";
In place where the traditional Auth'n'Auth token went, I'm getting the following error:
validation of the authentication token in api request failed.
Can someone help me out please ?!

Why the same Access Token works for Media but doesn't work for Feed?

I'm using InstaSharp to get all medias from my account. This code works without any error:
var user = new InstaSharp.Endpoints.Media.Authenticated(config, authInfo);
var media = user.Popular();
Now if I do the same with Users:
var user = new InstaSharp.Endpoints.Users.Authenticated(config, authInfo);
var feed = user.Feed("self");
This returns:
OAuthParameterException: The access_token provided is invalid.
Why the same access token works in one place but doesn't work in another?
Note: I'm passing all scopes (basic, comments, likes, relationships) while getting the access token.I have also tried with default (basic) and it didn't work either.
Note2: Since I'm trying this in a ConsoleApplication, the way I'm getting the access token is, generate the link, copy/paste to the browser, confirm the authorization and get it from url. I'm not sure it makes any difference but it's worth noting...
I figure out the problem. It seems that the generated link was requesting code, not access_token.So that's why it was invalid.
And I realized that I was using older version of InstaSharp even though I downloaded it from it's website. First, I installed the latest version via NuGet. Then, I get the access token and the media like this:
var config = new InstagramConfig(clientId, clientSecret);
var AuthLink = OAuth.AuthLink(config.OAuthUri + "/authorize",
config.ClientId,
config.RedirectUri,
scopes,
OAuth.ResponseType.Code);
var authInfo = new OAuth(config);
// get the code from the link then pass it to RequestToken
var response = await authInfo.RequestToken(code);
var user = new Users(config, response);
var media = await user.RecentSelf(count: 30);
And all worked very well.

Get Facebook's Application Access Token

I have to use Facebook's notification for my web app.
Facebook Graph API requires the Application Access Token for this action.
Is there a way to get this token by code (C# SDK) or is this generated by Facebook a single time?
Is this token static (and secret) or with expire datetime?
For info: https://developers.facebook.com/tools/access_token/ - App Token, not User Token!
Thanks
The answer is the dynamic way by code:
var fb = new FacebookClient();
dynamic result = fb.Get( "oauth/access_token", new
{
client_id = <myAppID>,
client_secret = <mySecretID>,
grant_type = "client_credentials"
} );
var apptoken = result.access_token;
Or by the combination or appid|secretid
You can just use the concatenation of id and secret with a pipe symbol in the middle:
app_id|app_secret
This is actually how the PHP SDK creates the app access token internally, so there should be no question about the reliability of this method. (From other endpoints where you actively query for an app access token you might get another token that does not match this scheme though.)
you can investigate the getApplicationAccessToken method as in another c# sdk project from github
https://github.com/barans/FacebookCsharpSdk/blob/master/FacebookCSharpSDK/FacebookClient/FacebookClient.cs

facebook c# sdk: deleting a request-id

I am using the latest facebook c# sdk (http://facebooksdk.codeplex.com/). After i have sent an apprequest, i want to delete the request id.
This is how i do it at the moment:
var app = new FacebookClient(appid, appsecret);
app.Delete(requestID);
But i am not sure if its get deleted or not. If i try to see if it still exist using the graph api i get:
{
"error": {
"type": "GraphMethodException",
"message": "Unsupported get request."
}
}
But the user still has the request in his notification area. So my question is> Is the request deleted, or did i miss something? Thanks
var url = "https://graph.facebook.com/{0}?access_token={1}";
fb.Delete((String.Format(url, fullRequestId, fb.AccessToken)));
First parameter is requestId and user id like -> fullRequestId = requestId + "_" + fbUser.id
Second parameter is Accesstoken
I'm just getting started on this myself, but I'm guessing that you need to instantiate the FacebookClient with the authorization code from the user, not with your application data. The way I understand it, the request is sent by the user not your application. Hence the need to use the users authorization code to get information about the requeset.
This is what's working for me (sorry it's VB.Net):
Dim fb As FacebookClient = New FacebookClient(Config.FacebookAppId,Config.FacebookAppSecret)
Dim result = fb.Delete(String.Format("{0}_{1}?access_token={2}", facebookRequestId, facebookUserId, fb.AccessToken))

Post comment on a status Facebook C# API

I'm trying to post a comment to a status on Facebook. Basically what I'm doing is something like this:
var parameters = new Dictionary<string, object>(); parameters["message"] = "hello";
fb.Post("/"+id+"/comments", parameters);
Where fb is a FacebookClient object and id is the id of the status.
Unfortunately this doesn't post the comment on recent status. For instance, if I type https://graph.facebook.com/"id"/comments in a web-browser it returns no data if the status is recent, but if the status is old (more than 1 month) it returns the information about the comments on that status.
Is there a way to comment on a status, picture, etc. using this API with C#?
the fb.Post command seems to be correct. I use the same (in vb.net) and it works like expected...
string AccessToken = "...." // User's access token
FacebookClient fb = new FacebookClient(AccessToken);
dynamic parameters = new ExpandoObject();
parameters.message = txtNewComment.Text.Trim();
dynamic result=fb.Post(HiddenMyPostID.Value+"/comments", parameters);
Above is code that i use for posting new comment on any post of facebook. And It's working.
I was having the same issue so I tried few things & this worked for me
var token = "[your access token]";
var fb = new Facebook.FacebookClient(token);
var postId = "173213306032925_74xxxxxxxxxxxxx"; //replace this with your big id which comprises of [userid]_[postid]
var parameters = new Dictionary<string, object>();
parameters.Add("message", "test message");
Console.WriteLine(fb.Post(id+"/comments", parameters).ToString()); // should give new comment's id
Console.WriteLine(fb.Get(postId +"/comments").ToString()); //should give you details
//for deleting
fb.Delete(newly_created_comment_id); //should return true or false
For this, you need to learn about how can you use Graph API and you also need to learn about some parameters like
height should be in integers which indicated the height of the pixels.
redirection should be in boolean and its default value should be true.
width is in integers which indicates the width of pictures in pixels.
There are many more but it can be understood by the standard programmatic interfaces.
When you use the programming language which you prefer should be based on HTTPS requests.
The version of APIs describes that all the requests are firstly in encrypted form and then they are sent via HTTPs requests.
To do so, you need to send make a registration for your application even users are not allowed to log in.
I hope it would work for you. But apart from it, you need to do more researches.
http://wholestatus.com/

Categories