【问题标题】:HTTP Post method is not working for yammer apiHTTP Post 方法不适用于 yammer api
【发布时间】:2015-11-27 10:51:05
【问题描述】:

我需要实现一个向用户发送邀请邮件的服务。 我关注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;
}

【问题讨论】:

    标签: c# api yammer


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 1970-01-01
      • 2023-03-24
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      相关资源
      最近更新 更多