Getting Access Token from facebook with publish_stream - c#

I tried this:
oAuthClient = new FacebookOAuthClient(FacebookApplication.Current);
oAuthClient.RedirectUri = callBack;
var parameters = new Dictionary<string, object>();
parameters.Add("permissions", "offline_access,publish_stream");
var loginUri = oAuthClient.GetLoginUrl(parameters);
this brings me the access token , i save it in Database
now i try to post to facebook doing this :
var postparameters = new Dictionary<string, object>();
postparameters["message"] = tweet;
postparameters["name"] = "This is a name";
_fbClient = new FacebookClient(accessToken);
var result = _fbClient.Post("/me/feed", postparameters);
problem is this tells me that im not authorized to post. so in order to make it work i have to visit this url :
http://www.facebook.com/authorize.php?api_key=[key]&v=1.0&ext_perm=publish_stream
this also have a problem cause this link doesnt redirect back !
so the question is how i do get the access token from the first place with the publish_stream permission?

change
parameters.Add("permissions", "offline_access,publish_stream");
to
parameters.Add("scope", "offline_access,publish_stream");

Related

C# Facebook app / post on site

im very new with Facebook apps and read several threads for creating them, but I have some problems with it.
First of all what I want to do: I want to create a web application that is able to post pictures, text and links on a facebook page that is managed by me.
I used the Facebook C# SDK: here!
What I have:
string facebookPageId = "<my page id>";
string app_id = "<my app id>";
string app_secret = "<my app secret>";
string scope = "publish_stream,manage_pages";
var fb = new FacebookClient();
dynamic res = fb.Get("oauth/access_token", new
{
client_id = app_id,
client_secret = app_secret,
grant_type = "client_credentials"
});
var access_token = res.access_token;
dynamic messagePost = new ExpandoObject();
messagePost.access_token = access_token;
messagePost.link = "http://www.test.at";
messagePost.name = "Testbot";
messagePost.caption = "{*actor*} " + "hello this is a test";
messagePost.description = "[SOME_DESCRIPTION]";
FacebookClient app = new FacebookClient(access_token);
app.AppId = app_id;
app.AppSecret = app_secret;
try
{
var result = app.Post("/hrechttest" + "/feed", messagePost);
}
catch (Exception e)
{
}
Well the code runs without any exceptions but in the output window I get the following:
Exception thrown: 'Facebook.FacebookOAuthException' in Facebook.dll
The next problem is:
As I understood it you must link your facebook app with your facebook page, but when I want to do that I cant select the page:
So what I did wrong or missed?
publish_stream is deprecated since many years, publish_pages is the correct permission to post to a Page (as Page).
API reference: https://developers.facebook.com/docs/graph-api/reference/page/feed#publish
Make sure you are using a Page Token, not a User Token:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
How to create Page Apps is explained in the docs too: https://developers.facebook.com/docs/pages/tabs

Updating a Password in RackSpace using C#

I'm just trying to change a password of the main account and a sub user in RackSpaceCloud using C# but I keep running into a UserNotAuthorized exception. Its weird because I can do anything else without this error, reset Api keys, list users and userID's(etc.). Sample Code
net.openstack.Core.Domain.CloudIdentity cloudIdentity = new CloudIdentity()//Admin Credits
{
Username = "me",
APIKey = "blahblahblah",
};
CloudIdentityProvider cloudIdentityProvider = new CloudIdentityProvider(cloudIdentity);
cloudIdentityProvider.SetUserPassword("correctUserID", "newP#ssw0rd", cloudIdentity);
And then I error which is confusing because methods like,
cloudIdentityProvider.ListUsers(cloudIdentity)
cloudIdentityProvider.ResetApiKey("UserID", cloudIdentity);
Work Perfectly. Any Help or Ideas would be appreciated.
Oh and Btw the addition info on the exception is always the same. "Unable to authenticate user and retrieve authorized service endpoints"
This is a bug. I have opened issue 528 but in the meantime here is a workaround.
var cloudIdentity = new CloudIdentity
{
Username = "{username}",
APIKey = "{api-key}"
};
var cloudIdentityProvider = new CloudIdentityProvider(cloudIdentity);
var userAccess = cloudIdentityProvider.Authenticate(cloudIdentity);
var request = new HttpRequestMessage(HttpMethod.Post, string.Format("https://identity.api.rackspacecloud.com/v2.0/users/{0}", userAccess.User.Id));
request.Headers.Add("X-Auth-Token", userAccess.Token.Id);
var requestBody = JObject.FromObject(new { user = new { username = userAccess.User.Name } });
((JObject)requestBody["user"]).Add("OS-KSADM:password", "{new-password}");
request.Content = new StringContent(requestBody.ToString(), Encoding.UTF8, "application/json");
using (var client = new HttpClient())
{
var response = client.SendAsync(request).Result;
}
The cloud identity used must be an admin if you need to change another user's password, otherwise non-admins may only change their own password.

Facebook login winforms

Here is my generate URL:
private Uri GenerateLoginUrl(string appId, string extendedPermissions)
{
// for .net 3.5
// var parameters = new Dictionary<string,object>
// parameters["client_id"] = appId;
dynamic parameters = new ExpandoObject();
parameters.client_id = appId;
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
// The requested response: an access token (token), an authorization code (code), or both (code token).
parameters.response_type = "token";
// list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
parameters.display = "popup";
//scope
parameters.scope = "Email";
// add the 'scope' parameter only if we have extendedPermissions.
if (!string.IsNullOrWhiteSpace(extendedPermissions))
parameters.scope = extendedPermissions;
// when the Form is loaded navigate to the login url.
return _fb.GetLoginUrl(parameters);
//webBrowser.Navigate(new Uri("www.facebook.com"));
}
The problem is the return _fb.GetLoginUrl is NULL. I have a webBrowser control already on the WinForm.
Any ideas? or can i just hard code the URL?
Worked at this a few months ago. I had the same problem cause by the ajax in the response site. The problem is that the ajax load the site content after the url is loaded. I fixed it by wait for the webbrowseres load event. I think it was webrowser.DocumentCompleted
I hope this help you.

How to get the Twitter Usertimeline with Tweetsharp

I try to get the last 20 statuses of the Usertimeline. So I search in the internet and get
the follow code:
TwitterUser twitterUser = TwitterUser.Show("Username").ResponseObject;
if (twitterUser != null)
{
UserTimelineOptions userTimelineOptions = new UserTimelineOptions();
userTimelineOptions.UserId = twitterUser.Id;
return TwitterTimeline.UserTimeline(userTimelineOptions).ResponseObject;
}
return null;
When I test it, I get the follow exception:
Unexpected token when deserializing object: StartObject. Line 1, position 1795.
I have no idea what's wrong so I hope you can help me!
Since Twitterizer is discontinued, I assumed that you moved to TweetSharp:
TwitterService service = new TwitterService("consumerKey", "consumerSecret");
service.AuthenticateWith("accessToken", "accessTokenSecret");
var options = new ListTweetsOnHomeTimelineOptions();
options.ExcludeReplies = true;
var tweets = service.ListTweetsOnHomeTimeline(options);
For Twitterizer:
UserTimelineOptions options = new UserTimelineOptions();
options.ScreenName = "Username";
var tweets = TwitterTimeline.UserTimeline(options).ResponseObject;
Twitterizer uses the 1.0 API and TweetSharp has the required oAuth for the 1.1 twitter API:
https://dev.twitter.com/blog/changes-coming-to-twitter-api
Try this
var twitterService = new TwitterService("consumerKey", "consumerSecret");
twitterService.AuthenticateWith("token", "tokenSecret");
var tweets = twitterService.ListTweetsOnHomeTimeline();

Posting a message to a wall from code behind

I am trying to post a comment from code behind. Is there a way to do this?
I can get the users profile pic by
http://graph.facebook.com/" + fbId + "/picture;
Can I post to a wall in a similar way?
Thnaks
Using facebook custom application and Facebook SDK
var facebookClient = new FacebookClient(FbConf.testimonialsAppId, FbConf.testimonialsAppSecret);
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("message","message on the wall"));
parameters.Add("created_time", DateTime.Now.ToString());
dynamic result = facebookClient.Post(string.Format("{0}/feed", FbConf.testimonialsAppId), parameters);
var id = result.id;

Categories