Facebook graph api Facebook图形API照片

Facebook graph api Facebook图形API照片,facebook-graph-api,photo,Facebook Graph Api,Photo,我正在尝试使用ASP.NET和C#将照片上传到Facebook。使用FacebookOAuth登录效果很好,访问令牌是正确的,但我就是不知道如何使用它发布照片。非常感谢您提供的任何帮助,您很难理解Facebooks API和ASP.NET是如何协同工作的。您可以使用现成的组件 或者使用以下代码(从源代码获得) // ///将照片发布到特定相册,并将其张贴在用户的墙上 /// ///照片将出版 ///这张照片的留言 ///已发布照片的照片ID 公共字符串PublishPhoto(System.

我正在尝试使用ASP.NET和C#将照片上传到Facebook。使用FacebookOAuth登录效果很好,访问令牌是正确的,但我就是不知道如何使用它发布照片。非常感谢您提供的任何帮助,您很难理解Facebooks API和ASP.NET是如何协同工作的。

您可以使用现成的组件

或者使用以下代码(从源代码获得)

//
///将照片发布到特定相册,并将其张贴在用户的墙上
/// 
///照片将出版
///这张照片的留言
///已发布照片的照片ID
公共字符串PublishPhoto(System.Drawing.Bitmap photo、字符串消息、字符串AccessToken)
{
string AlbumID=“me”//贴到墙上,无特殊相册
System.IO.MemoryStream MS=新的System.IO.MemoryStream();
保存照片(MS,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[]Imagebytes=MS.ToArray();
Dispose女士();
//设置用于构造多部分/表单数据的基本变量
字符串换行符=“\r\n”;
字符串边界=DateTime.Now.Ticks.ToString(“x”);
字符串数据=”;
//构造数据
数据+=“-”+边界+换行符;
数据+=“内容配置:表单数据;名称=\“消息\”“+换行符+换行符;
数据+=消息+换行符;
数据+=“-”+边界+换行符;
数据+=“内容配置:表单数据;文件名=\”test.jpg\”+换行符;
数据+=“内容类型:图像/jpeg”+换行符+换行符;
字符串结尾=换行符+“--”+边界符+“--”+换行符;
//将数据转换为字节[]数组
System.IO.MemoryStream finaldatastream=new System.IO.MemoryStream();
byte[]databytes=System.Text.Encoding.UTF8.GetBytes(数据);
byte[]endingbytes=System.Text.Encoding.UTF8.GetBytes(结束);
写入(数据字节,0,数据字节,长度);
写入(Imagebytes,0,Imagebytes.Length);
写入(endingbytes,0,endingbytes.Length);
字节[]finaldatabytes=finaldatastream.ToArray();
finaldatastream.Dispose();
//提出请求
System.Net.WebRequest request=System.Net.HttpWebRequest.Create(string.Format(“https://graph.facebook.com/{0}/photos?access_token={1},AlbumID,AccessToken));
request.ContentType=“多部分/表单数据;boundary=“+boundary;
request.ContentLength=finaldatabytes.Length;
request.Method=“POST”;
使用(System.IO.Stream RStream=request.GetRequestStream())
{
Write(finaldatabytes,0,finaldatabytes.Length);
}
System.Net.WebResponse WR=request.GetResponse();
字符串_Response=“”;
使用(System.IO.StreamReader sr=new System.IO.StreamReader(WR.GetResponseStream())
{
_响应=sr.ReadToEnd();
}
string Id=”“;//从响应中获取Id
返回Id;
}

谢谢!但是我在设置所需的属性时遇到了问题,你有什么例子可以说明我如何做到这一点吗?
/// <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;
}