Facebook Web API exception

本文关键字:exception API Web Facebook | 更新日期: 2023-09-27 17:55:11

我不断收到异常 远程服务器返回错误:(400) 错误请求。每当它来到HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;.有人可以帮我吗?这是我的代码。顺便说一下,它只是一个没有html内容的aspx。它只是一个纯 C# 文件:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;
namespace Facebook_API
{
public partial class Facebooksync : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAuthorization();
    }
    private void CheckAuthorization()
    {
        string app_id = "1234567891234567"; //Just placed this digits to keep this hidden
        string app_secret = "12345678912345678912345678912345"; //Just placed this digits to keep this hidden
        string scope ="publish_stream,manage_pages"; //Scope are the permissions
        if( Request["code"] == null)
        {
            Response.Redirect(String.Format(
                "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                app_id, Request.Url.AbsoluteUri, scope));
        }
        else
        {
            Dictionary<string, string> tokens = new Dictionary<string, string>();
            string url = string.Format(
                "https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret{4}",
                app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string vals = reader.ReadToEnd();
                foreach(string token in vals.Split('&'))
                {
                    tokens.Add(token.Substring(0, token.IndexOf("=")),
                    token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1 ));
                }
            }
            string access_token = tokens["access_token"];
            var client = new FacebookClient(access_token);
            client.Post("/me/feed", new { message = "Testing Facebook WebAPI " });
        }
    }
}

您也可以通过简单地创建一个新项目并创建一个 Web 表单来尝试此代码,只需将其插入 cs 文件中即可。

Facebook Web API exception

你为什么要使用

request.Method = "Put";

而不是正常的GET请求?