2011-02-04 18 views
5

का उपयोग कर .NET से फेसबुक दीवार पर छवि पोस्ट करना मैं एक एप्लिकेशन विकसित करने के लिए फेसबुक्स जावास्क्रिप्ट एपीआई का उपयोग कर रहा हूं जिसे उपयोगकर्ता की दीवार पर एक छवि पोस्ट करने में सक्षम होना आवश्यक है। ऐप के उस भाग को सर्वर-साइड होने की आवश्यकता है जहां तक ​​मैं कह सकता हूं, क्योंकि इसे छवि डेटा को "मल्टीपार्ट/फॉर्म-डेटा" के रूप में पोस्ट करने की आवश्यकता है।ग्राफ़ एपीआई

नोट: यह "पोस्ट" का उपयोग करके सरल संस्करण नहीं है, बल्कि असली "फोटो" विधि है।

http://graph.facebook.com/me/photos

मुझे लगता है कि मैं दो समस्याओं, एक .NET और एक फेसबुक समस्या का सामना कर रहा हूँ:

फेसबुक समस्या: मैं काफी यकीन नहीं है अगर सभी मापदंडों बहुखण्डीय के रूप में भेज दिया जाना चाहिए/फॉर्म-डेटा (access_token और संदेश सहित)। सीयूआरएल उपयोग/आवेदन का उपयोग करने वाला एकमात्र कोड उदाहरण है।

नेट समस्या: मैं नेट से बहुखण्डीय/फार्म डेटा अनुरोधों कभी नहीं जारी किए गए हैं, और मुझे यकीन है कि नेट स्वचालित रूप से माइम-भागों बनाता है, तो नहीं कर रहा हूँ, या मैं कुछ में पैरामीटर एन्कोड करने के लिए है, तो विशेष तरीका।

डीबग करना थोड़ा मुश्किल है, क्योंकि ग्राफ एपीआई से मुझे केवल त्रुटि प्रतिक्रिया "400 - खराब अनुरोध" है। नीचे यह कोड है जब मैंने यह प्रश्न लिखने का फैसला किया था (हाँ, यह थोड़ा वर्बोज़ है :-)

अंतिम उत्तर निश्चित रूप से एक नमूना स्निपेट होगा जो .NET से एक छवि पोस्ट कर रहा है, लेकिन मैं बस सकता हूं कम में।

string username = null; 
string password = null; 
int timeout = 5000; 
string requestCharset = "UTF-8"; 
string responseCharset = "UTF-8"; 
string parameters = ""; 
string responseContent = ""; 

string finishedUrl = "https://graph.facebook.com/me/photos"; 

parameters = "access_token=" + facebookAccessToken + "&message=This+is+an+image"; 
HttpWebRequest request = null; 
request = (HttpWebRequest)WebRequest.Create(finishedUrl); 
request.Method = "POST"; 
request.KeepAlive = false; 
//application/x-www-form-urlencoded | multipart/form-data 
request.ContentType = "multipart/form-data"; 
request.Timeout = timeout; 
request.AllowAutoRedirect = false; 
if (username != null && username != "" && password != null && password != "") 
{ 
    request.PreAuthenticate = true; 
    request.Credentials = new NetworkCredential(username, password).GetCredential(new Uri(finishedUrl), "Basic"); 
} 
//write parameters to request body 
Stream requestBodyStream = request.GetRequestStream(); 
Encoding requestParameterEncoding = Encoding.GetEncoding(requestCharset); 
byte[] parametersForBody = requestParameterEncoding.GetBytes(parameters); 
requestBodyStream.Write(parametersForBody, 0, parametersForBody.Length); 
/* 
This wont work 
byte[] startParm = requestParameterEncoding.GetBytes("&source="); 
requestBodyStream.Write(startParm, 0, startParm.Length); 
byte[] fileBytes = File.ReadAllBytes(Server.MapPath("images/sample.jpg")); 
requestBodyStream.Write(fileBytes, 0, fileBytes.Length); 
*/ 
requestBodyStream.Close(); 

HttpWebResponse response = null; 
Stream receiveStream = null; 
StreamReader readStream = null; 
Encoding responseEncoding = System.Text.Encoding.GetEncoding(responseCharset); 
try 
{ 
    response = (HttpWebResponse) request.GetResponse(); 
    receiveStream = response.GetResponseStream(); 
    readStream = new StreamReader(receiveStream, responseEncoding); 
    responseContent = readStream.ReadToEnd(); 
} 
finally 
{ 
    if (receiveStream != null) 
    { 
     receiveStream.Close(); 
    } 
    if (readStream != null) 
    { 
     readStream.Close(); 
    } 
    if (response != null) 
    { 
     response.Close(); 
    } 
} 

उत्तर

4

यहां बाइनरी डेटा अपलोड करने का नमूना है। लेकिन/me/photos पर अपलोड करने से छवि को दीवार में प्रकाशित नहीं किया जाएगा :(छवि आपके ऐप के एल्बम में सहेजी जा रही है। मैं फ़ीड में इसे कैसे घोषित करने के बारे में अटक गया हूं। फिर भी एक और तरीका है कि एक छवि को "वॉल" में पोस्ट करना है एल्बम ", यूआरएल ==" ग्राफ.facebook.com/%wall-album-id%/photos "। लेकिन इस तरह के एल्बम बनाने के लिए कोई रास्ता नहीं मिला (उपयोगकर्ता साइट के माध्यम से एक छवि अपलोड करते समय इसे बनाता है) ।

{ 
    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); 
    uploadRequest = (HttpWebRequest)WebRequest.Create(@"https://graph.facebook.com/me/photos"); 
    uploadRequest.ServicePoint.Expect100Continue = false; 
    uploadRequest.Method = "POST"; 
    uploadRequest.UserAgent = "Mozilla/4.0 (compatible; Windows NT)"; 
    uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary; 
    uploadRequest.KeepAlive = false; 

    StringBuilder sb = new StringBuilder(); 

    string formdataTemplate = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n"; 
    sb.AppendFormat(formdataTemplate, boundary, "access_token", PercentEncode(facebookAccessToken)); 
    sb.AppendFormat(formdataTemplate, boundary, "message", PercentEncode("This is an image")); 

    string headerTemplate = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n"; 
    sb.AppendFormat(headerTemplate, boundary, "source", "file.png", @"application/octet-stream"); 

    string formString = sb.ToString(); 
    byte[] formBytes = Encoding.UTF8.GetBytes(formString); 
    byte[] trailingBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); 

    long imageLength = imageMemoryStream.Length; 
    long contentLength = formBytes.Length + imageLength + trailingBytes.Length; 
    uploadRequest.ContentLength = contentLength; 

    uploadRequest.AllowWriteStreamBuffering = false; 
    Stream strm_out = uploadRequest.GetRequestStream(); 

    strm_out.Write(formBytes, 0, formBytes.Length); 

    byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)imageLength))]; 
    int bytesRead = 0; 
    int bytesTotal = 0; 
    imageMemoryStream.Seek(0, SeekOrigin.Begin); 
    while ((bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length)) != 0) 
    { 
     strm_out.Write(buffer, 0, bytesRead); bytesTotal += bytesRead; 
     gui.OnUploadProgress(this, (int)(bytesTotal * 100/imageLength)); 
    } 

    strm_out.Write(trailingBytes, 0, trailingBytes.Length); 

    strm_out.Close(); 

    HttpWebResponse wresp = uploadRequest.GetResponse() as HttpWebResponse; 
} 
+0

धन्यवाद @fltz तक नहीं पहुंचा जा सकता है। अपने उदाहरण को अनुकूलित करके इसे काम करने के लिए प्रबंधित किया गया (कुछ वार्स जहां स्निपेट के दायरे से बाहर घोषित किया गया था)। –

+0

क्या आपका आवेदन दीवार पर एक छवि पोस्ट करता है या इसे एप्लिकेशन के एल्बम में अपलोड करता है? मेरे परीक्षणों में दीवार/फ़ीड पर अपलोड की गई छवि की कोई दृष्टि नहीं थी। – fltz

+1

मैं अभी तक इसके आसपास नहीं मिला है। अब तक मैं इसे केवल दीवार पर देखता हूं जब एप्लिकेशन के लिए डिफ़ॉल्ट एल्बम बनाया जाता है। लेकिन [इस पोस्ट] के अनुसार (http://www.raywenderlich.com/1626/how-to-post-to-a-users-wall-upload-photos-and-add-a-like-button-from- आपका-आईफोन-ऐप) यह संभव होना चाहिए। –

1

आपको बाइट एरे का उपयोग करके मल्टीपार्ट/फॉर्म-डेटा स्वयं बनाना होगा। वैसे भी मैंने पहले ही यह कर लिया है। आप http://computerbeacon.net/ पर फेसबुक ग्राफ टूलकिट देख सकते हैं। मैं कुछ दिनों में टूलकिट को संस्करण 0.8 में अपडेट करूंगा, जिसमें यह "फेसबुक फोटो पर फोटो पोस्ट करें" फ़ंक्शन के साथ-साथ अन्य नई सुविधाएं और अपडेट शामिल होंगे।

+0

धन्यवाद - एक स्पिन के लिए यह लेने के लिए देख रहे हैं। –

+0

इस साइट पर – zchpit

3

वर्ग पद्धति का उपयोग करके @ Fitz के कोड। एक बाइट सरणी या छवि के लिए एक फ़ाइल पथ में उत्तीर्ण कर लें। एक एल्बम आईडी पास करें, अगर मौजूदा एल्बम में अपलोड करने तक साफ़ किया।

public string UploadPhoto(string album_id, string message, string filename, Byte[] bytes, string Token) 
{ 
    // Create Boundary 
    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); 

    // Create Path 
    string Path = @"https://graph.facebook.com/"; 
    if (!String.IsNullOrEmpty(album_id)) 
    { 
     Path += album_id + "/"; 
    } 
    Path += "photos"; 

    // Create HttpWebRequest 
    HttpWebRequest uploadRequest; 
    uploadRequest = (HttpWebRequest)HttpWebRequest.Create(Path); 
    uploadRequest.ServicePoint.Expect100Continue = false; 
    uploadRequest.Method = "POST"; 
    uploadRequest.UserAgent = "Mozilla/4.0 (compatible; Windows NT)"; 
    uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary; 
    uploadRequest.KeepAlive = false; 

    // New String Builder 
    StringBuilder sb = new StringBuilder(); 

    // Add Form Data 
    string formdataTemplate = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n"; 

    // Access Token 
    sb.AppendFormat(formdataTemplate, boundary, "access_token", HttpContext.Current.Server.UrlEncode(Token)); 

    // Message 
    sb.AppendFormat(formdataTemplate, boundary, "message", message); 

    // Header 
    string headerTemplate = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n"; 
    sb.AppendFormat(headerTemplate, boundary, "source", filename, @"application/octet-stream"); 

    // File 
    string formString = sb.ToString(); 
    byte[] formBytes = Encoding.UTF8.GetBytes(formString); 
    byte[] trailingBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); 
    byte[] image; 
    if (bytes == null) 
    { 
     image = File.ReadAllBytes(HttpContext.Current.Server.MapPath(filename)); 
    } 
    else 
    { 
     image = bytes; 
    } 

    // Memory Stream 
    MemoryStream imageMemoryStream = new MemoryStream(); 
    imageMemoryStream.Write(image, 0, image.Length); 

    // Set Content Length 
    long imageLength = imageMemoryStream.Length; 
    long contentLength = formBytes.Length + imageLength + trailingBytes.Length; 
    uploadRequest.ContentLength = contentLength; 

    // Get Request Stream 
    uploadRequest.AllowWriteStreamBuffering = false; 
    Stream strm_out = uploadRequest.GetRequestStream(); 

    // Write to Stream 
    strm_out.Write(formBytes, 0, formBytes.Length); 
    byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)imageLength))]; 
    int bytesRead = 0; 
    int bytesTotal = 0; 
    imageMemoryStream.Seek(0, SeekOrigin.Begin); 
    while ((bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length)) != 0) 
    { 
     strm_out.Write(buffer, 0, bytesRead); bytesTotal += bytesRead; 
    } 
    strm_out.Write(trailingBytes, 0, trailingBytes.Length); 

    // Close Stream 
    strm_out.Close(); 

    // Get Web Response 
    HttpWebResponse response = uploadRequest.GetResponse() as HttpWebResponse; 

    // Create Stream Reader 
    StreamReader reader = new StreamReader(response.GetResponseStream()); 

    // Return 
    return reader.ReadToEnd(); 
} 
1

मैं एक था फ़ोटो पोस्ट करने के लिए

// url example: https://graph.facebook.com/you/photos?access_token=YOUR_TOKEN 
request.AddFile("source", imageAsByteArray, openFileDialog1.SafeFileName, getMimeType(Path.GetExtension(openFileDialog1.FileName))); 
request.addParameter("message", "your photos text here"); 

User API या Page API

How to convert Image to Byte Array

नोट:: RestSharp का उपयोग कर तस्वीरें पोस्ट करने के लिए ble मैं कोई रिक्त स्ट्रिंग गुजर रहा था के रूप में माइम प्रकार और फेसबुक बहुत चालाक यह पता लगाने के लिए किया गया ।

0

शायद उपयोगी

 [TestMethod] 
     [DeploymentItem(@".\resources\velas_navidad.gif", @".\")] 
     public void Post_to_photos() 
     { 
      var ImagePath = "velas_navidad.gif"; 
      Assert.IsTrue(File.Exists(ImagePath)); 

      var client = new FacebookClient(AccessToken); 
      dynamic parameters = new ExpandoObject(); 

      parameters.message = "Picture_Caption"; 
      parameters.subject = "test 7979"; 
      parameters.source = new FacebookMediaObject 
{ 
    ContentType = "image/gif", 
    FileName = Path.GetFileName(ImagePath) 
}.SetValue(File.ReadAllBytes(ImagePath)); 

      //// Post the image/picture to User wall 
      dynamic result = client.Post("me/photos", parameters); 
      //// Post the image/picture to the Page's Wall Photo album 
      //fb.Post("/368396933231381/", parameters); //368396933231381 is Album id for that page. 

      Thread.Sleep(15000); 
      client.Delete(result.id); 
     } 

संदर्भ: Making Requests

संबंधित मुद्दे