简体   繁体   中英

WP7 Facebook SDK Sample Code Doesn't Work

Does the current WP7 sample project work? I've downloaded it and entered my app id and secret key, which I know to work from a previous WM6.5 app. I get the facebook login page, and I log in. I get the page asking if I want to grant permission, which I do.

The code then throws a KeyNotFoundException while looking for the "access_token" key. I've peppered the code with:

if (objectname.ContainsKey("access_code")) ...

I've put this everywhere I can see an attempt being made to search for this key, to no avail. The code still errors at the same point.

Has something changed on the facebook side since this sample code was last tested successfully? Is there something else I should be doing?

Many thanks

Here's a example how to use the Facebook SDK with WP7. So basically I got webBrowser1 on MainPage.xaml but at default it's hidden.

Code behind:

void MainPage_Loaded(object sender, RoutedEventArgs e)
{

            string appId = "";
            string[] extendedPermissions = new[] { "publish_stream", "offline_access", "user_groups" };

            var oauth = new FacebookOAuthClient { AppId = appId };

            var parameters = new Dictionary<string, object>
                {
                    { "response_type", "token" },
                    { "display", "touch" }
                };

            if (extendedPermissions != null && extendedPermissions.Length > 0)
            {
                var scope = new StringBuilder();
                scope.Append(string.Join(",", extendedPermissions));
                parameters["scope"] = scope.ToString();
            }

            var loginUrl = oauth.GetLoginUrl(parameters);
            webBrowser1.Navigate(loginUrl);
            webBrowser1.Visibility = System.Windows.Visibility.Visible;
            webBrowser1.Navigated += webBrowser1_Navigated;
}
void webBrowser1_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        FacebookOAuthResult result;
        if (FacebookOAuthResult.TryParse(e.Uri.AbsoluteUri, out result))
        {
            if (result.IsSuccess)
            {
                string _accessToken = result.AccessToken;
                webBrowser1.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                var errorDescription = result.ErrorDescription;
                var errorReason = result.ErrorReason;
            }
        }
    }

I think this might be the answer you're looking for:

http://facebooksdk.codeplex.com/discussions/284103

see also: http://facebooksdk.codeplex.com/workitem/5925

var jsonObject = new JsonObject();

if (returnParameter.ContainsKey("access_token"))
{
    jsonObject["access_token"] = returnParameter["access_token"];
}

If you're just logging in then there isn't a parameter called access_code that you need.

I suspect you need to be using access_token which is returned as part of the fragment after a login attempt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM