2012-05-07 90 views
1

我正嘗試使用Graph API將照片發佈到Facebook。我設法發佈消息給我的時間表,但不是照片。 Facebook調試器不提供任何錯誤,對象屬性也是正確的。Facebook Graph API ASP.NET Post image

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")); 

我試過幾天讓它帶有照片工作,而不是,我是什麼什麼想法做錯了?

回答

1

我可以建議看一下FB C#SDK(可用here或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(); 
} 
+0

感謝。它現在正在工作,但現在我有另一個問題;我需要做些什麼才能使該應用程序適用於其他用戶?我是否需要像Facebook的發佈權限一樣獲得批准? –

+1

請檢查 - https://developers.facebook.com/docs/reference/javascript/ - 基本上,您需要傳遞您的應用程序ID並向FB.Init()調用請求「publish_stream」權限。另一位用戶將登錄 - 並會看到您的應用程序的請求。如果他同意,那麼您將獲得一個適用於該用戶的應用令牌 - 因此您可以在自己的牆上發帖等。 – avs099