2012-04-21 6 views
0

Я пытаюсь загрузить фотографию в Facebook с помощью ASP.NET и C#. Вход в систему с использованием Facebook oAuth работает отлично, и токен доступа правильный, но я просто не могу понять, как его использовать для публикации фотографии. Любая помощь приветствуется, она действительно пытается понять, как работают API Facebook и ASP.NET.Facebook Graph API Photos

ответ

0

вы можете использовать готовую компоненту

http://computerbeacon.net/library/960n60e000000

Или используйте следующий код (получил из исходного кода)

/// <summary> 
/// Publishes a photo to a specific album and post it on the user's wall 
/// </summary> 
/// <param name="photo">photo to be published</param> 
/// <param name="message">message with this photo</param> 
/// <returns>PhotoID of the photo published</returns> 
public string PublishPhoto(System.Drawing.Bitmap photo, string message, string AccessToken) 
{ 
    string AlbumID = "me";//post to wall , no spesific album 
    System.IO.MemoryStream MS = new System.IO.MemoryStream(); 
    photo.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg); 
    byte[] Imagebytes = MS.ToArray(); 
    MS.Dispose(); 

    //Set up basic variables for constructing the multipart/form-data data 
    string newline = "\r\n"; 
    string boundary = DateTime.Now.Ticks.ToString("x"); 
    string data = ""; 

    //Construct data 
    data += "--" + boundary + newline; 
    data += "Content-Disposition: form-data; name=\"message\"" + newline + newline; 
    data += message + newline; 

    data += "--" + boundary + newline; 
    data += "Content-Disposition: form-data; filename=\"test.jpg\"" + newline; 
    data += "Content-Type: image/jpeg" + newline + newline; 

    string ending = newline + "--" + boundary + "--" + newline; 

    //Convert data to byte[] array 
    System.IO.MemoryStream finaldatastream = new System.IO.MemoryStream(); 
    byte[] databytes = System.Text.Encoding.UTF8.GetBytes(data); 
    byte[] endingbytes = System.Text.Encoding.UTF8.GetBytes(ending); 
    finaldatastream.Write(databytes, 0, databytes.Length); 
    finaldatastream.Write(Imagebytes, 0, Imagebytes.Length); 
    finaldatastream.Write(endingbytes, 0, endingbytes.Length); 
    byte[] finaldatabytes = finaldatastream.ToArray(); 
    finaldatastream.Dispose(); 

    //Make the request 
    System.Net.WebRequest request = System.Net.HttpWebRequest.Create(string.Format("https://graph.facebook.com/{0}/photos?access_token={1}", AlbumID, AccessToken)); 
    request.ContentType = "multipart/form-data; boundary=" + boundary; 
    request.ContentLength = finaldatabytes.Length; 
    request.Method = "POST"; 
    using (System.IO.Stream RStream = request.GetRequestStream()) 
    { 
     RStream.Write(finaldatabytes, 0, finaldatabytes.Length); 
    } 
    System.Net.WebResponse WR = request.GetResponse(); 
    string _Response = ""; 
    using (System.IO.StreamReader sr = new System.IO.StreamReader(WR.GetResponseStream())) 
    { 
     _Response = sr.ReadToEnd(); 
    } 
    string Id = "";//get the Id from the Response 
    return Id; 
} 
+0

Спасибо! Но у меня проблемы с настройкой необходимых свойств, есть ли у вас какой-нибудь пример того, как я могу это сделать? –