FBQL - api key is invalid

Mar 13, 2008 at 9:46 AM
I'm trying to develop for Bebo and have some problems getting the Facebook Toolkit to work for it. So I made a subset of the FacebookAPI class and tried to use it on Bebo. This didn't work but when I tried to use it on Facebook it didn't work there either. Could anyone point me to what I do wrong? I run the class like this:

SNQLWrapper snqlWrapper = new SNQLWrapper(Request.Params"fb_sig_user", Request.Params"fb_sig_session_key");
myDiv.InnerText = snqlWrapper.GetFriendsXML();

and get this response:

<?xml version="1.0" encoding="UTF-8" ?>
<error_response>
<errorcode>101</errorcode>
<errormsg>api key is invalid</errormsg>
<request_args list="true">
<arg>
<key>v</key>
<value>1.0</value>
</arg>
<arg>
<key>query</key>
<value>SELECT uid2 FROM friend WHERE uid1=XXX</value>
</arg>
<arg>
<key>api_key</key>
<value>fd34f88b72a4fe9c4ebfc2e47adcd879</value>
</arg>
<arg>
<key>method</key>
<value>facebook.fql.query</value>
</arg>
<arg>
<key>call_id</key>
<value>8ca52c24932e020</value>
</arg>
<arg>
<key>session_key</key>
<value>7dad1ad3d71ec9879ef4d5f1-575014824</value>
</arg>
<arg>
<key>sig</key>
<value>d815096b6af14f737b9f4b4c6182474f</value>
</arg>
</request_args>
</error_response>


This is class I put together:

public class SNQL
{
public SNQL(string userId, string sessionKey)
{
_userId = userId;
_sessionKey = sessionKey;
}

private string _userId = "";
private string _sessionKey = "";

public string applicationKey = Utilities.FaceBookConstants.FACEBOOKAPI_KEY;
private const string VERSION = "1.0";

public string GetFriendsXML()
{
Dictionary<string, string> parameterList = new Dictionary<string, string>(3);
parameterList.Add("method", "facebook.fql.query");

if (!string.IsNullOrEmpty(_userId))
{
parameterList.Add("query",
String.Format(CultureInfo.InvariantCulture, "{0}{1}",
"SELECT uid2 FROM friend WHERE uid1=", _userId));
}
else
{
throw new Exception("User Id is required");
}

return ExecuteApiCallString(parameterList, true);
}

private string GetQueryResponse(string requestUrl, string postString)
{
// Create a web request for input path.
WebRequest webRequest = WebRequest.Create(requestUrl);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";

if (!String.IsNullOrEmpty(postString))
{
byte[] parameterString = Encoding.ASCII.GetBytes(postString);
webRequest.ContentLength = parameterString.Length;

using (Stream buffer = webRequest.GetRequestStream())
{
buffer.Write(parameterString, 0, parameterString.Length);
buffer.Close();
}
}

WebResponse webResponse = webRequest.GetResponse();

string responseData;
using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
{
responseData = streamReader.ReadToEnd();
}
return responseData;
}

private string CreateHTTPParameterList(IDictionary<string, string> parameterList)
{
StringBuilder queryBuilder = new StringBuilder();

parameterList.Add("api_key", _applicationKey);
parameterList.Add("v", VERSION);
parameterList.Add("call_id", DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture));
parameterList.Add("sig", GenerateSignature(parameterList));

// Build the query
foreach (KeyValuePair<string, string> kvp in parameterList)
{
queryBuilder.Append(kvp.Key);
queryBuilder.Append("=");
queryBuilder.Append(HttpUtility.UrlEncode(kvp.Value));
queryBuilder.Append("&");
}
queryBuilder.Remove(queryBuilder.Length - 1, 1);

return queryBuilder.ToString();
}

private string ExecuteApiCallString(IDictionary<string, string> parameterDictionary, bool useSession)
{
if (useSession)
{
parameterDictionary.Add("session_key", _sessionKey);
}
string requestUrl = GetRequestUrl(parameterDictionary"method" == "auth.getSession");
string parameters = CreateHTTPParameterList(parameterDictionary);
return GetQueryResponse(requestUrl, parameters);
}

private string GetRequestUrl(bool useSSL)
{
string returnValue = "http://apps.bebo.com/restserver.php";
if (useSSL)
{
returnValue = returnValue.Replace("http", "https");
}
return returnValue;
}

/// <summary>
/// This method generates the signature based on parameters supplied
/// </summary>
/// <param name="parameters">List of paramenters</param>
/// <returns>Generated signature</returns>
private string GenerateSignature(IDictionary<string, string> parameters)
{
StringBuilder signatureBuilder = new StringBuilder();
// Sort the keys of the method call in alphabetical order
List<string> keyList = ParameterDictionaryToList(parameters);
keyList.Sort();
// Append all the parameters to the signature input paramaters
foreach (string key in keyList)
signatureBuilder.Append(String.Format(CultureInfo.InvariantCulture, "{0}={1}", key, parameterskey));

// Append the secret to the signature builder
signatureBuilder.Append(Utilities.FaceBookConstants.FACEBOOKSECRETKEY);
byte[] hash;

MD5 md5 = MD5.Create();
// Compute the MD5 hash of the signature builder
hash = md5.ComputeHash(Encoding.UTF8.GetBytes(
signatureBuilder.ToString().Trim()));

// Reinitialize the signature builder to store the actual signature
signatureBuilder = new StringBuilder();

// Append the hash to the signature
foreach (byte hashByte in hash)
signatureBuilder.Append(hashByte.ToString("x2", CultureInfo.InvariantCulture));

return signatureBuilder.ToString();
}

private List<string> ParameterDictionaryToList(IEnumerable<KeyValuePair<string, string>> parameterDictionary)
{
List<string> parameters = new List<string>();

foreach (KeyValuePair<string, string> kvp in parameterDictionary)
{
parameters.Add(String.Format(CultureInfo.InvariantCulture, "{0}", kvp.Key));
}
return parameters;
}
}
Aug 18, 2008 at 3:53 AM
Hi, just wanted to know if you have had any luck getting the Facebook Toolkit working with Bebo?

Thank a lot,
Phill
Coordinator
Aug 19, 2008 at 4:52 AM
Its really hard to tell you what might be wrong.  I have trouble following what you are doing.  I'd prefer to try to help you use the toolkit rather than help you fix your modified version.  Can you post some more details about what problems you ran into.
Aug 19, 2008 at 9:16 PM
Hi, oh no - I was just wondering if anyone has had any luck using this toolkit with the Bebo Social Networking API - as they basically licenced the Facebook API for their applications as well. Before I start to make a Bebo app with this toolkit - I was just wondering if this was possible,

Thanks,
Phill