user1983379
user1983379

Reputation: 101

HTTP Post method is not working for yammer api

I need to implement a service for sending invitation mail to users. I am following Samlman's artice. I am trying to get a permanent token for Yammer API using the post method shown below.

I'm currently getting:

Error: 404 "server not found error".

There is no inner exception.

Do I need to pass authHeader?

If yes, then how do I send the 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;
}

Upvotes: 0

Views: 290

Answers (1)

mr i.o
mr i.o

Reputation: 952

The information in that blog is incorrect, it does not work and it is unsupported by Yammer. I'd advise you pre-obtain the token using a service account, store it somewhere safe, then pass it as an Authorization header in your POST and/or GET request. A very good example is shown here - http://blogs.technet.com/b/israelo/archive/2015/02/24/consuming-yammer-restful-api-with-angularjs-for-dummies.aspx

Upvotes: 1

Related Questions