C# Yammer API-附件上传
我正在尝试上传文件,同时向Yammer发布一些消息。发布消息有效(所有授权内容等都可以)。当我要附加文件时,会出现问题。 我试着从这里开始遵循代码: 但我担心我遗漏了一些部分(无法引用整个项目,不知为什么我的VisualStudio有问题) 这就是为什么我试图遵循指定请求参数的传统方法。我把我的方法放在下面:C# Yammer API-附件上传,c#,yammer,C#,Yammer,我正在尝试上传文件,同时向Yammer发布一些消息。发布消息有效(所有授权内容等都可以)。当我要附加文件时,会出现问题。 我试着从这里开始遵循代码: 但我担心我遗漏了一些部分(无法引用整个项目,不知为什么我的VisualStudio有问题) 这就是为什么我试图遵循指定请求参数的传统方法。我把我的方法放在下面: public bool postMessage(string body, string attachement) { var url = "https://www.
public bool postMessage(string body, string attachement)
{
var url = "https://www.yammer.com/api/v1/messages.json";
NameValueCollection parameters = new NameValueCollection();
parameters.Add("body", body);
parameters.Add("group_id", group_id);
var authzHeader = oauth.GenerateAuthzHeader(url, "POST");
//new request
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Headers.Add("Authorization", authzHeader);
request.Method = "POST";
//content type settings
request.ContentType = "multipart/form-data";
request.KeepAlive = true;
//Proxy settings
request.Proxy = new System.Net.WebProxy("my company's proxy", true);
request.Proxy.Credentials = new System.Net.NetworkCredential(user, password);
//prepare the parameters
FileInfo fi = new FileInfo(attachement);
int i = 0;
long postDataSize = fi.Length;
parameters.Add("attachment", "attachment1");
parameters.Add("file", Path.GetFileName(attachement));
int count = 0;
string wdata = string.Empty;
foreach (string key in parameters.Keys)
{
if (count == 0)
{
wdata = key + "=" + oauth.encode(parameters[key]);
}
else
{
wdata += "&" + key + "=" + oauth.encode(parameters[key]);
}
count++;
}
//add the parameters
byte[] postDataBytes = Encoding.ASCII.GetBytes(wdata);
request.ContentLength = postDataBytes.Length + postDataSize;
Stream reqStream = request.GetRequestStream();
reqStream.Write(postDataBytes, 0, postDataBytes.Length);
//write the file
//postDataBytes = Encoding.ASCII.GetBytes(fileHeader);
//reqStream.Write(postDataBytes, 0, postDataBytes.Length);
int bufferSize = 10240;
FileStream readIn = new FileStream(attachement, FileMode.Open, FileAccess.Read);
readIn.Seek(0, SeekOrigin.Begin); // move to the start of the file
byte[] fileData = new byte[bufferSize];
int bytes;
while ((bytes = readIn.Read(fileData, 0, bufferSize)) > 0)
{
// read the file data and send a chunk at a time
reqStream.Write(fileData, 0, bytes);
}
readIn.Close();
reqStream.Close();
using (var response = (System.Net.HttpWebResponse)request.GetResponse())
{
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
{
string resp = reader.ReadToEnd();
return true;
}
}
}
代码可能看起来有点混乱,但我希望你能理解。
主要问题是:
- 当我只发布一条消息(group_id,body)时,它就起作用了
- 当我尝试上述方法并发布附件时,我从Yammer获得“内部服务器错误”李>
有人知道如何使用API将文件上传到Yammer吗?peferrally in.NET:)可以使用MultipartFormDataContent类型将二进制内容发布到Yammer messages.json REST端点 发布大量图像、文本和标记的工作示例:
WebProxy proxy = new WebProxy()
{
UseDefaultCredentials = true,
};
HttpClientHandler httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
};
using (var client = new HttpClient(httpClientHandler))
{
using (var multipartFormDataContent = new MultipartFormDataContent())
{
string body = "Text body of message";
var values = new[]
{
new KeyValuePair<string, string>("body", body),
new KeyValuePair<string, string>("group_id", YammerGroupID),
new KeyValuePair<string, string>("topic1", "Topic ABC"),
};
foreach (var keyValuePair in values)
{
multipartFormDataContent.Add(new StringContent(keyValuePair.Value), String.Format("\"{0}\"", keyValuePair.Key));
}
int i = 1;
foreach (Picture p in PictureList)
{
var FileName = string.Format("{0}.{1}", p.PictureID.ToString("00000000"), "jpg");
var FilePath = Server.MapPath(string.Format("~/images/{0}", FileName));
if (System.IO.File.Exists(FilePath))
{
multipartFormDataContent.Add(new ByteArrayContent(System.IO.File.ReadAllBytes(FilePath)), '"' + "attachment" + i.ToString() + '"', '"' + FileName + '"');
i++;
}
}
var requestUri = "https://www.yammer.com/api/v1/messages.json";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
var result = client.PostAsync(requestUri, multipartFormDataContent).Result;
}
}
WebProxy proxy=newwebproxy()
{
UseDefaultCredentials=true,
};
HttpClientHandler HttpClientHandler=新的HttpClientHandler()
{
Proxy=代理,
};
使用(var客户端=新的HttpClient(httpClientHandler))
{
使用(var multipartFormDataContent=new multipartFormDataContent())
{
string body=“消息的文本体”;
var值=新[]
{
新的KeyValuePair(“主体”,主体),
新的KeyValuePair(“组id”,YammerGroupID),
新的KeyValuePair(“主题1”、“主题ABC”),
};
foreach(值中的var keyValuePair)
{
添加(新的StringContent(keyValuePair.Value),String.Format(“\{0}\”,keyValuePair.Key));
}
int i=1;
foreach(图片列表中的图片p)
{
var FileName=string.Format(“{0}.{1}”,p.PictureID.ToString(“00000000”),“jpg”);
var FilePath=Server.MapPath(string.Format(“~/images/{0}”,FileName));
if(System.IO.File.Exists(FilePath))
{
multipartFormDataContent.Add(新的ByteArrayContent(System.IO.File.ReadAllBytes(FilePath)),“+”附件“+i.ToString()+”,“+FileName+”);
i++;
}
}
var requestUri=”https://www.yammer.com/api/v1/messages.json";
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,AccessToken);
var result=client.PostAsync(requestUri,multipartFormDataContent).result;
}
}
你能把这归结为最简单的不可行的事情吗?我认为,添加有关文件流的部分是问题的根源。我假设它在Yammer端产生了一个内部服务器错误,所以我猜我是在某种程度上(显然?)做错了。好吧,我找到了一个可能的解决方案-我不必要地坚持主题中的Yammer API,而问题是与请求代码本身有关,不是API@MattHTTPWebRequest多部分链接解决了您的问题吗?什么是PictureList?