使用捕获的访问令牌检索用户的 Facebook 信息时出现问题

发布于 2024-12-10 17:21:20 字数 1982 浏览 3 评论 0 原文

我一直在尝试编写一个与 facebook 交互的 Windows 窗体应用程序,以检索有权获取某些用户信息的访问令牌。我一直在尝试使用以下代码获取自己的生日,但它不断给我带来 400 bad request 错误。基本上,运行此代码并在身份验证登录后,它应该显示一个包含用户生日的消息框。在本例中,我在 api.GET 方法中使用我自己的用户 ID。这似乎是访问令牌问题,因为当我不传入任何令牌时,我可以使用相同的代码查看公共可用信息,例如 id,但我打印出访问令牌进行检查,似乎没问题。任何帮助将不胜感激。第一次在这里发帖

public partial class AccessTokenRetrieval : Form
{
    private string accessToken=null;


    public AccessTokenRetrieval()
    {
        InitializeComponent();
    }

    private void accessTokenButton_Click(object sender, EventArgs e)
    {
        string getAccessTokenURL = @"https://graph.facebook.com/oauth/authorize?client_id=223055627757352&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup&grant_type=client_credentials&scope=user_photos,offline_access";
        getAccessTokenWebBrowser.Navigate(getAccessTokenURL);
    }

    private void getAccessTokenWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {

        string successUrl = @"http://www.facebook.com/connect/login_success.html";

        string urlContainingUserAuthKey = e.Url.ToString();
        MessageBox.Show(urlContainingUserAuthKey);

        int searchInt = urlContainingUserAuthKey.IndexOf(successUrl);
        MessageBox.Show(searchInt.ToString());

        if (urlContainingUserAuthKey.IndexOf(successUrl) == -1)
        {
            string accessTokenString;
            accessTokenString = Regex.Match(urlContainingUserAuthKey, "access_token=.*&").ToString();
            this.accessToken = accessTokenString.Substring(13, accessTokenString.Length - 14);
            //100001067570373
            //MessageBox.Show(accessToken);
            accessTokenTextBox.Text = this.accessToken;
            Facebook.FacebookAPI api = new Facebook.FacebookAPI(this.accessToken);
            JSONObject me = api.Get("/100001067570373");
            MessageBox.Show(me.Dictionary["user_birthday"].String);
        }
    }
#

I have been attempting to code a windows form application that interacts with facebook to retrieve the access token that has permissions to get some of the user's information. I have been trying to get the birthday of myself using the following code but it keeps giving me the 400 bad request error. Basically after running this code, and logging in at the authentication it is suppose to show a messagebox containing the user's birthday. In this case, I am using my own user id in the api.GET method. It seems to be the access token issue as when I don't pass in any tokens, i can view public available information such as id using the same code but I print out the access token to check and it seems to be alright. Any help would be much appreciated. First time posting here

public partial class AccessTokenRetrieval : Form
{
    private string accessToken=null;


    public AccessTokenRetrieval()
    {
        InitializeComponent();
    }

    private void accessTokenButton_Click(object sender, EventArgs e)
    {
        string getAccessTokenURL = @"https://graph.facebook.com/oauth/authorize?client_id=223055627757352&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup&grant_type=client_credentials&scope=user_photos,offline_access";
        getAccessTokenWebBrowser.Navigate(getAccessTokenURL);
    }

    private void getAccessTokenWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {

        string successUrl = @"http://www.facebook.com/connect/login_success.html";

        string urlContainingUserAuthKey = e.Url.ToString();
        MessageBox.Show(urlContainingUserAuthKey);

        int searchInt = urlContainingUserAuthKey.IndexOf(successUrl);
        MessageBox.Show(searchInt.ToString());

        if (urlContainingUserAuthKey.IndexOf(successUrl) == -1)
        {
            string accessTokenString;
            accessTokenString = Regex.Match(urlContainingUserAuthKey, "access_token=.*&").ToString();
            this.accessToken = accessTokenString.Substring(13, accessTokenString.Length - 14);
            //100001067570373
            //MessageBox.Show(accessToken);
            accessTokenTextBox.Text = this.accessToken;
            Facebook.FacebookAPI api = new Facebook.FacebookAPI(this.accessToken);
            JSONObject me = api.Get("/100001067570373");
            MessageBox.Show(me.Dictionary["user_birthday"].String);
        }
    }

#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-12-17 17:21:20

我请求您尝试 http://facebooksdk.codeplex.com 并查看示例文件夹。
它包括 WinForms 身份验证示例以及向 Facebook 发出各种请求的示例。

以下是我建议您阅读的其他有用链接。

http://blog.prabir.me /post/Facebook-CSharp-SDK-Writing-your-first-Facebook-Application.aspx

http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx

I would request you to try http://facebooksdk.codeplex.com and checkout the samples folder.
It includes sample for WinForms authentication and also making various request to Facebook.

Here are other useful links that I would recommend you to read.

http://blog.prabir.me/post/Facebook-CSharp-SDK-Writing-your-first-Facebook-Application.aspx

http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文