1

GraphAPIを使用して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 に答える