Facebook canvas app with Silverlight 3 can't find HttpUtility
I've been having this severely frustrating issue with the 5.1.1 of the Facebook C# SDK. I have loaded the CSSilverlightFacebookApp successfully and confirmed that my Facebook settings are fine. I was able to get the test app to show up.
For my hosting scenario, I'm using the following Google App Engine example:
def load_signed_request(signed_request):
"""Load the user state from a signed_request value"""
global APP_ID, APP_SECRET
try:
sig, payload = signed_request.split(u'.', 1)
sig = base64_url_decode(sig)
data = json.loads(base64_url_decode(payload))
expected_sig = hmac.new(
APP_SECRET, msg=payload, digestmod=hashlib.sha256).digest()
# allow the signed_request to function for upto 1 day
if sig == expected_sig and \
data[u'issued_at'] > (time.time() - 86400):
return (data, data.get(u'user_id'), data.get(u'oauth_token'))
except ValueError, ex:
pass # ignore if can't split on dot
class MainHandler(webapp.RequestHandler):
def post(self):
global APP_ID, APP_SECRET
(ignore, ignore, oauth_token) = load_signed_request(self.request.get('signed_request'))
if oauth_token:
path = os.path.join(os.path.dirname(__file__), 'templates/silverlight.html')
params = dict(access_token=oauth_token)
self.response.out.write(template.render(path, params))
That code seems to work fine, I get the oauth_token passed into my Silverlight code. The code below works up to the last line:
var token = "";
if (App.Current.Resources.Contains("token") && App.Current.Resources["token"] != null)
token = App.Current.Resources["token"].ToString();
if (!string.IsNullOrEmpty(token))
{
fb = new Faceb开发者_开发百科ookClient(token);
fb.GetCompleted += (o, args) =>
{
if (args.Error == null)
{
var result = (IDictionary<string, object>)args.GetResultData();
//Dispatcher.BeginInvoke(() => InfoBox.ItemsSource = result);
}
else
{
// TODO: Need to let the user know there was an error
//failedLogin();
}
};
// Making Facebook call here!
fb.GetAsync("/me");
}
On the fb.GetAsync("/me"), I get a TypeLoadException saying it can't find HttpUtility:
System.TypeLoadException was unhandled by user code
Message=Could not load type 'System.Net.HttpUtility' from assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
StackTrace:
at FluentHttp.HttpHelper.UrlEncode(String s)
at Facebook.FacebookUtils.GetUrl(IDictionary`2 domainMaps, String name, String path, IDictionary`2 parameters)
at Facebook.FacebookClient.GetUrl(String name, String path, IDictionary`2 parameters)
at Facebook.FacebookClient.BuildRootUrl(HttpMethod httpMethod, String path, IDictionary`2 parameters)
at Facebook.FacebookClient.PrepareRequest(String path, IDictionary`2 parameters, HttpMethod httpMethod, Stream& input, IDictionary`2& mediaObjects)
at Facebook.FacebookClient.ApiAsync(String path, IDictionary`2 parameters, HttpMethod httpMethod, Object userToken)
at Facebook.FacebookClient.GetAsync(String path, IDictionary`2 parameters, Object userToken)
at Facebook.FacebookClient.GetAsync(String path, IDictionary`2 parameters)
at Facebook.FacebookClient.GetAsync(String path)
at TicTacToe10.Page.Page_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException:
I have confirmed that I have included System.Windows.dll and System.Net.dll. What could the problem be? It looks to me like my code is exactly like the CSFacebookSilverlightApp example. I also thought it could be related to me using Silverlight 3 instead of 4, but I've tried all combinations of 3 and 4 with the Facebook.dll for sl3 and sl4.
Turns out, my Silverlight project was missing the SILVERLIGHT compilation symbol (probably because I originally created it in MonoDevelop). This caused the Facebook SDK to look in the wrong place for HttpUtility. Here's the code from the Facebook SDK that pointed me to the solution:
public static string UrlEncode(string s)
{
#if WINDOWS_PHONE
return System.Net.HttpUtility.UrlEncode(s);
#elif SILVERLIGHT
return System.Windows.Browser.HttpUtility.UrlEncode(s);
#else
return UrlEncode(s, Encoding.UTF8);
#endif
}
精彩评论