httpwebrequest - How can i post a message into yammer group from c# -
i trying post message yammer group c# restapi. tried many links available not working.
can provide exact code that.
all codes partially available , stuck proceed.
thanks
this quick draft of how it;
public static string postmessage(string data, int groupid, string accesstoken) { // build request-uri var endpoint = "https://www.yammer.com/api/v1/messages.json"; var sb = new stringbuilder(endpoint); if (endpoint.contains("?")) sb.append("&access_token=" + accesstoken); else sb.append("?access_token=" + accesstoken); var uri = new uri(sb.tostring()); var request = webrequest.create(uri) httpwebrequest; // create request if (request == null) result = "it failed."; // add request properties request.headers.add("authorization", "bearer " + accesstoken); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; // format data var dataarr = data.split(' '); var postdata = "body=" + string.join("+", dataarr); postdata += "&group_id=" + groupid; byte[] bytes = encoding.utf8.getbytes(postdata); request.contentlength = bytes.length; // fire away, , read response try { var requeststream = request.getrequeststream(); requeststream.write(bytes, 0, bytes.length); var response = request.getresponse(); var stream = response.getresponsestream(); if (stream != null) { var reader = new streamreader(stream); var response = reader.readtoend(); stream.dispose(); reader.dispose(); result = response; } } catch (webexception e) { var response = e.response; // handle exception. } return result; // return json result }
an important thing notice request here using authorization header in request, , including "bearer " in there, rather in url; no longer valid.
when comes posting group, can see use of "group_id" body parameter of post.
hope helps! best of luck.
Comments
Post a Comment