1

我正在尝试使用 Graph API 将照片发布到 Facebook。我设法在我的时间轴上发布了一条消息,但不是一张照片。Facebook 调试器没有给出错误并且对象属性是正确的。

public class oAuthFacebook
{
    public enum Method { GET, POST };

    public const string AUTHORIZE = "https://graph.facebook.com/oauth/authorize";

    public const string ACCESS_TOKEN = "https://graph.facebook.com/oauth/access_token";

    public const string CALLBACK_URL ="http://test.com/FbCallback.aspx";


    private string _consumerKey = "";

    private string _consumerSecret = "";

    private string _token = "";



 public string ConsumerKey
{

    get
    {
        if (_consumerKey.Length == 0)
        {
            _consumerKey = "";
        }
        return _consumerKey;
    }
    set { _consumerKey = value; }
}


public string ConsumerSecret 
{
    get
    {
        if (_consumerSecret.Length == 0)
        {
            _consumerSecret = "";
        }
        return _consumerSecret;
    }
    set { _consumerSecret = value; }

    }

public string Token { get { return _token; } set { _token = value; } }





public string AuthorizationLinkGet()
{
    return string.Format("{0}?client_id={1}&redirect_uri={2}&,publish_actions",
  AUTHORIZE, this.ConsumerKey, CALLBACK_URL);
}

public void AccessTokenGet(string authToken)
{
    this.Token = authToken;
    string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri=
  {2}&client_secret={3}&code={4}",
    ACCESS_TOKEN, this.ConsumerKey, CALLBACK_URL, this.ConsumerSecret, authToken);

    string response = WebRequest(Method.GET, accessTokenUrl, String.Empty);

    if (response.Length > 0)
    {
        NameValueCollection qs = HttpUtility.ParseQueryString(response);

        if (qs["access_token"] != null)
        {
            this.Token = qs["access_token"];
        }
    }
}

public string WebRequest(Method method, string url, string postData)
{
    HttpWebRequest webRequest = null;
    StreamWriter requestWriter = null;
    string responseData = "";
    webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
    webRequest.Method = method.ToString();
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.UserAgent = "http://test.com";
    webRequest.Timeout = 40000;

    if (method == Method.POST)
    {
        webRequest.ContentType = "application/x-www-form-urlencoded";

        requestWriter = new StreamWriter(webRequest.GetRequestStream());
        try
        {
            requestWriter.Write(postData);
        }
        catch
        {
            throw;
        }
        finally
        {
            requestWriter.Close();
            requestWriter = null;
        }
    }
    responseData = WebResponseGet(webRequest);
    webRequest = null;
    return responseData;
}



public string WebResponseGet(HttpWebRequest webRequest)
{
    StreamReader responseReader = null;
    string responseData = "";
    try
    {
        responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
        responseData = responseReader.ReadToEnd();
    }
    catch
    {
        throw;
    }
    finally
    {
        webRequest.GetResponse().GetResponseStream().Close();
        responseReader.Close();
        responseReader = null;
    }
    return responseData;
}

这就是我发送测试消息的方式,它工作正常:

var json = oAuth.WebRequest(oAuthFacebook.Method.POST, url, "message=" +
HttpUtility.UrlEncode("Testmessage"));

我已经尝试了几天让它与照片一起使用,对我做错了什么有什么想法吗?

4

1 回答 1

1

我可以建议查看 FB C# SDK(在此处或 NuGet 中可用)而不是发送原始请求。这是我用来将图像上传到 FB 相册的功能(您应该知道相册 ID,或者您也可以创建它,或者您可以枚举所有相册并获取您需要的相册):

public static string UploadPhoto(string imageName, string albumID, string accesstoken, string photoComment = null, bool doNotPostStory = false)
{
    var fbAPI = new FacebookApp(accesstoken);
    var p = new FacebookMediaObject {FileName = path};

    p.SetValue( <<YOUR IMAGE GOES HERE AS byte[]>>);

    p.ContentType = "multipart/form-data";

    var param = new Dictionary<string, object> { {"attachment", p} };
    if (!string.IsNullOrEmpty(photoComment))
        param.Add("message", photoComment);

    // http://stackoverflow.com/questions/7340949/is-it-possible-to-upload-a-photo-to-fanpage-album-without-publishing-it
    // http://developers.facebook.com/blog/post/482/
    if (doNotPostStory == true)
    {
        param.Add("no_story", "1");
    }

    var result = fbAPI.Post(string.Format("http://graph.facebook.com/{0}/photos", albumID), param);
    return result.ToString();
}
于 2012-05-08T14:03:38.840 回答