Mathieu Fosse
Mathieu Fosse

Reputation: 1

Getting information about a Facebook Page via API but without a session_key

I'm going to create a Facebook application which get informations (name and page_url) about Facebook page from his page_id. So i went to the Facebook Wiki and i found this API method which sounds good at first look :

http://wiki.developers.facebook.com/index.php/Pages.getInfo

My only wish was that i wouldn't pass a session_key parameter because i don't want to force the facebook user to install the application. (You know the "allow application" page or popup).

But the description says :

"The session_key parameter is optional. When the session_key parameter is not passed, you can get information only for Pages that have added your application."

So no problem, exactly what i'm looking for !

But after a try, i get this error :

"102 The session key was improperly submitted or has reached its timeout. Direct the user to log in again to obtain another key."

Normal, because i don't pass the session_key parameter.

I'm using Rails + Facebooker plugin. So i try this methods in my console without success :

>>Facebooker::Session.create.pages(:fields => ["name", "page_url"], :page_ids => "123456")
=> []


>> Facebooker::Session.create.fql_query("SELECT name, page_url FROM page WHERE page_id=123456")
=> []

(of course, the page with page_id="123456" has installed my application)

Someone knows how to get information about a page but without the needed of passing a session_key parameter ?

I know it's possible because i have already saw a facebook application retrieve information about a page without ask user to install his application.

Upvotes: 0

Views: 2284

Answers (3)

Mathieu Fosse
Mathieu Fosse

Reputation: 1

Okay, after some try out, I can say that the API method "Pages.getInfo" when it called without session_key seems to return ONLY pages already PUBLISHED.

And of course, pages that i want to get information might be UNPUBLISHED... too bad.

Any chance to retrieve a page url from a unpublished page ?

Upvotes: 0

Naresh
Naresh

Reputation: 31

private string ToUrlBase64String(byte[] Input)
{
    return Convert.ToBase64String(Input).Replace("=", String.Empty)
                                        .Replace('+', '-')
                                        .Replace('/', '_');
}

private byte[] SignWithHmac(byte[] dataToSign, byte[] keyBody)
{
    using (var hmacAlgorithm = new HMACSHA256(keyBody))
    {
        hmacAlgorithm.ComputeHash(dataToSign);
        return hmacAlgorithm.Hash;
    }
}

#region["Parse Signed_Request Method"]
/// <summary>
/// Parse Signed_Request Method
/// </summary>
/// <param name="signedRequestValue"></param>
/// <returns></returns>
private string ParseSignedRequest(string signedRequestValue)
{
    string PageID = string.Empty;
    string applicationSecret = "7f7e426af4d835a2fd54abea04520283";
    string[] signedRequest = signedRequestValue.ToString().Split('.');
    string expectedSignature = signedRequest[0];
    string payload = signedRequest[1];
    // Attempt to get same hash
    var Hmac = SignWithHmac(UTF8Encoding.UTF8.GetBytes(payload), UTF8Encoding.UTF8.GetBytes(applicationSecret));
    var HmacBase64 = ToUrlBase64String(Hmac);

    if (HmacBase64 != expectedSignature)
        return null;
    var encoding = new UTF8Encoding();
    var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
    var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
    string myvalue = Convert.ToString(JsonConvert.DeserializeObject(encoding.GetString(base64JsonArray)));        
    string ss = myvalue.Replace("{", "").Replace("}", "").Replace("\"", "");
    string[] s = ss.Split(',');
    for (int i = 0; i < s.Length; i++)
    {
        string[] s1 = s[i].Split(':');
        if (s1[0].ToString().Trim().ToLower() == "profile_id")
        {
            string[] myapp = s[i].Split(':');
            PageID = myapp[1].ToString().Trim();
        }
    }
    return PageID;

}

#endregion


#region["Page_Load Event"]
/// <summary>
/// Page_Load Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{

    string SignedRequest = Request.Form["signed_request"].ToString();
    string myvalue = Convert.ToString(ParseSignedRequest(SignedRequest));
    //now myvalue contain the page id      
}
#endregion

Upvotes: 1

rampr
rampr

Reputation: 1947

This thread on developers.facebook.com could help you http://forum.developers.facebook.com/viewtopic.php?pid=147246#p147246

Upvotes: 0

Related Questions