HTTP Post方法不适用于yammer api

时间:2015-11-27 10:51:05

标签: c# api yammer

我需要实现一项服务,以便向用户发送邀请邮件。 我关注Samlman's artice。 我正在尝试使用下面显示的post方法获取Yammer API的永久令牌。

我目前正在:

  

错误:404"服务器未找到错误"。

没有内在的例外。

我是否需要通过authHeader

如果是,那我该如何发送authHeader

string authUrl1 = string.Format("https://www.yammer.com/session?client_id={0}" , CLIENT_ID);
string postBody = string.Format(
        "{0}{1}{2}{3}{4}{5}{6}",
        "utf8=%E2%9C%93&authenticity_token=",
        System.Web.HttpUtility.UrlEncode(authToken),
        "&network_permalink=&login=",
        HttpUtility.UrlEncode(userName),
        "&password=",
        pwd,
        "&remember_me=off");

//make the first post for code
postResults = MakePostRequest(postBody, authUrl1);
private static string MakePostRequest(string postBody, string url, string authHeader = null, string contentType = null)
{
    string results = string.Empty;

    try
    {
        //get the session and yamtrack cookie
        SetCookies();

        wr = WebRequest.CreateHttp(url);
        wr.Method = "POST";
        wr.CookieContainer = cc;

        //if an authHeader was provided, add it as a Bearer token to the request
        if (!string.IsNullOrEmpty(authHeader))
            wr.Headers.Add("Authorization", "Bearer " + authHeader);

        byte[] postByte = Encoding.UTF8.GetBytes(postBody);

        if (string.IsNullOrEmpty(contentType))
            wr.ContentType = "application/x-www-form-urlencoded";
        else
            wr.ContentType = contentType;

        wr.ContentLength = postByte.Length;
        Stream postStream = wr.GetRequestStream();
        postStream.Write(postByte, 0, postByte.Length);
        postStream.Close();

        wResp = (HttpWebResponse)wr.GetResponse();
        postStream = wResp.GetResponseStream();
        StreamReader postReader = new StreamReader(postStream);

        results = postReader.ReadToEnd();

        postReader.Close();
        postStream.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error in MakePostRequest: " + ex.Message);
    }
    return results;
}

1 个答案:

答案 0 :(得分:1)

该博客中的信息不正确,不起作用,Yammer不支持。我建议您使用服务帐户预先获取令牌,将其存储在安全的地方,然后将其作为POST和/或GET请求中的Authorization标头传递。这里显示了一个非常好的例子 - http://blogs.technet.com/b/israelo/archive/2015/02/24/consuming-yammer-restful-api-with-angularjs-for-dummies.aspx