How to get pageId for pages the facebook user is an admin of? Using facebook connect - c#

The user authenticates using Facebook Connect and once that's done I want to retrieve the pageIds for the pages he is a fan of. Once that's done; I want to publish something to the wall of that facebook page.
I found methods describing how to publish to a page in the facebook documentation; but those methods require a pageId variable. I'm not sure how to get that variable since my app uses facebook connect and is not installed "in" the facebook page.
I know this is possible since Seesmic Desktop (formerly Twhirl) has similar functionality.
Does anyone know how to do this?

As of 3/10/10, there is no method to quickly discover what pages a user is an admin for. However, you can use a single FQL call, see below.
SELECT page_id FROM page_admin WHERE uid = *UID*
in PHP, this looks like:
$this->fb = new Facebook('api_key', 'api_secret');
$fb_uid = $this->fb->get_loggedin_user();
$query = "SELECT page_id FROM page_admin WHERE uid = " . $fb_uid;
try{
$array = $this->fb->api_client->fql_query($query);
return $array;
} catch (Exception $e){
return false;
}
this will return a PHP array like this
Array
(
[0] => Array
(
[page_id] => 12341234
)
[1] => Array
(
[page_id] => 43214321
)
)

I don't know if you've solved this problem but I was looking into doing something similar with php. I've been reading the facebook api wiki and trying stuff out with their test console and I found that if you use the pages.getInfo and only ask for the page_id it'll give you a list of pages that the user is a fan of. After that it's just a matter of checking if their the admin to that page and figure if they want to post to it.
I'd give you some code but we're not working in the same language and I haven't implemented anything yet. (I figure this might have been resolved by you since the question is pretty old but I figured that I'd answer seeing how you were one of the top google results, in case someone else stumbled upon it.)

Related

Check if user ID exists in Discord server in c#

I'm trying to make a auth link that the user will open then to get the client id from the user and check if the user is exists in a server. I'm not sure what lib should I use would love to get help thank you. I saw this code but this is in JavaScript and I need it in c# + the part of auth link
USER_ID = '123123123';
if (guild.member(USER_ID)) {
// there is a GuildMember with that ID
}```
DSharpPlus is a fairly simple library to use. It'll throw 404 not found when the user isn't found so you'll need to check the ClientErrored event for failure.
var guild = //get current guild somehow
var user = await guild.GetMemberAsync(USERID);
Documentation on GetMemberAsync: https://dsharpplus.emzi0767.com/api/DSharpPlus.Entities.DiscordGuild.html#DSharpPlus_Entities_DiscordGuild_GetMemberAsync_System_UInt64_
Repo: https://github.com/DSharpPlus/DSharpPlus
D#+ vs Discord.Net is DNet will give you more control over handling of events and messages. D#+ is more plug and play kind of deal.

Skybrud social Log in via Facebook?

I'm usinng skybrud social to allow users to log into my site via Facebook, but am having a problem.
For some reason, the response never contains anything other than the Name and Id of the user... everything else is null.
var url = client.GetAuthorizationUrl(state, "public_profile", "email");
var service = FacebookService.CreateFromAccessToken(userAccessToken);
FacebookMeResponse user = service.Methods.Me();
Has anyone experienced this before? What could be the problem?
Facebook has multiple versions of their Graph API. In the most recent version (2.4), less fields are returned by default, and you instead have to tell the API to return the fields that you need. What version of the API you're using depends on the time you registered your app with Facebook.
Based on your code, it seems that you're using an older version of Skybrud.Social. If you update to the most recent version (0.9.4.1), you can do something like this:
// Declare the options for the call to the API
FacebookGetUserOptions options = new FacebookGetUserOptions("me") {
Fields = "name,email,gender"
};
// Make the call to the API
FacebookUserResponse response = service.Users.GetUser(options);
Hope this answers your questions ;)

Post to Facebook group via graph api c#

I am trying to post to a facebook group via the graph api in c#.
https://developers.facebook.com/docs/graph-api/reference/v2.2/group/feed
According to the api I can post a message as well as a link to a url, here is my code to try and do this:
Uri result;
bool X = Uri.TryCreate(url, UriKind.Absolute, out result)
if(X){
// POST to group FB
dynamic fbInfo = fb.Post("/v2.2/" + "groupID" + "/feed", new
{
message = websiteDesc,
link = url
});
var fbInfoJson = fbInfo.ToString();
}
First i check that the url is absolute and if so proceed to post to the facebook group.
so far this code does post to the group but only the message and not the link.
How can I get it to post the link?
Also the api says that I can include a photo to the post but it must be a string, can i assume this is the url of the image?
Thanks in advance :)
I am not entirely sure, but the docs say "Either link or message must be supplied" so maybe you can only post a message OR a link. Definitely worth to try. It must be an absolute URL, of course. Same goes for the picture.
Posting in a group is pretty hard nowadays anyway, since you would need user_groups and publish_actions for that - and you will not get user_groups approved so you canĀ“t use it for a public App. It will only work for users with a role in the App (Admin, Developer, Tester).

php session variable not saving

I am quite new to php and am trying to send data from c# to a php script which then posts to facebook.
The trouble is, after I receive the data I can't store it in the session array.
Here's what I've got:
session_start();
if($_POST['Facebook'])
{
echo 'DataReceived';
$HighScore = $_POST['Facebook'];
$_SESSION[1] = $HighScore;
}
$_SESSION['foo'] = 'bar';
Further down, after checking if the user is not logged into facebook I have:
$login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream",));
echo $login_url;
I then use this url in c# to open the user's browser where they can login to facebook through my facebook app.
Now, as far as I can tell, the $login_url will allow the the user to log into facebook then go back to this php page, this is where I lose the data that I sent from c#.
Also, after using var_dump($_SESSION) to check what was there and it contained $_SESSION['foo'], some facebook session variables, but not the highscore.
Are you sure you are getting the $_POST['Facebook'] Facebook parameter in $_POST because I see no problem in the code.

Problem getting access_token after migrating to OAuth 2.0

I have tried migrating my app to the OAuth 2.0 routine. I am having trouble getting the access_token from the cookie set by the JavaScript API. I decode the information in the cookie, but instead of an access_token and the user information I get a code. This seems like a rather weird change.
Is there any workaround for this, because it seems that you can't get your code exchanged to an access_token when you haven't specified a redirect_uri when you acquired the code.
I have considered just taking the access_token from the response in the JavaScript API and storing it in a cookie, but that kinda defeats the whole purpose of the extended security and I wanted to ask if there was a proper way to do it.
Could be that I am doing something wrong though, and if that is the case please tell me :)
EDIT
I am aware that the cookie holds a signed request, but according to the docs that signed request should hold the information I require like access_token and uid, but in my instance it only holds the code. That is basically the part I don't understand.
Turns out that (even though it is not documented) we need to exchange the code for an access_token ourselves. I think this is a total waste since that was the nice thing about the old cookie. It was fast and easy to get the access_token.
Anyway. To get the access_token from the new cookie you need to do the following:
public string ReturnAccessToken()
{
HttpCookie cookie = htc.Request.Cookies[string.Format("fbsr_{0}", facebookAppID)];
string jsoncode = System.Text.ASCIIEncoding.ASCII.GetString(FromBase64ForUrlString(cookie.Value.Split(new char[] { '.' })[1]));
JsonData data = JsonMapper.ToObject(jsoncode);
getAccessToken(data["code"].ToJson()
}
private string getAccessToken(string code)
{
//Notice the empty redirect_uri! And the replace on the code we get from the cookie.
string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}", "YOUR_APP_ID", "", "YOUR_APP_SECRET", code.Replace("\"", ""));
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
System.Net.HttpWebResponse response = null;
using (response = request.GetResponse() as System.Net.HttpWebResponse)
{
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
string retVal = reader.ReadToEnd();
return retVal;
}
}
public byte[] FromBase64ForUrlString(string base64ForUrlInput)
{
int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
result.Append(String.Empty.PadRight(padChars, '='));
result.Replace('-', '+');
result.Replace('_', '/');
return Convert.FromBase64String(result.ToString());
}
This may seem a bit redundant, but I suppose you can store the access_token in a session variable. If you do this and iFrame the your app on Facebook you need to know that it will not work in IE 6, 7 and 8 if the user have set his browser privacy settings to medium. There is a workaround for this, but as it is not a part of this question I will not write it. If people really want it, write a comment and I will show it :)
-----------------------------------EDIT------------------------------------------
When using any of the old IE browsers you can't use cookies or session variables in pages that are Iframed in, like your pages on Facebook. This is a problem that can't really be solved sufficiently in coding. By sufficiently I mean that the solution is not nice. You need to set the p3p-header in your response. You can of course do this in coding for all the pages that you service, but the easiest solution (if you are using a .NET server to host your pages) is to set up a p3p policy for the IIS. A guide for this can be seen in http://support.microsoft.com/kb/324013. It shouldn't matter what you write in the p3p policy (if you check Facebooks own you can see that they use "We don't have a p3p policy), the important part is that there stands something. I have had troubles just using random text though, but if you use the text in the example there shouldn't be a problem :)
This took me forever to find out, so I hope someone can use it :D
Unfortunately I don't have the answer directly, but I do have a documentation bug that I filed against facebook in order to try to get the documentation there: http://bugs.developers.facebook.net/show_bug.cgi?id=20363
I have a similar problem that when I try to decode the signedRequest from the authResponse of FB.login, they payload contains something like:
{"algorithm":"HMAC-SHA256","code":"THE_CODE_HERE","issued_at":1315433244,"user_id":"THE_USER_ID"}
As you stated, the docs do talk about how to turn that code into an access_token. That appears to be in the "Server Side" documentation here: http://developers.facebook.com/docs/authentication/
If you grab the accessToken from FB.login you can get it from the js and cache it, but as you said, that isn't actually signed, and could relatively easily be faked.
And you're right, this doesn't appear to have any of the useful information that's described here: developers.facebook.com/docs/authentication/signed_request/ (http removed since I don't have enough reputation points yet to post more than 2 links - sorry)
Perhaps you can vote up my bug? I'll post this link on that bug too.
fbsr_APP_ID cookie is actually a signed_request, check out facebook official docs how do you decode signed request verify signature and get the user information. You can look also at official php SDK source how they get access token from there.
You have to use the code to get the actual access_token.

Categories