Search code examples
yammerunauthorized

Integrating Yammer gives me 401 error


I am trying to integrate Yammer with our website. Eg: Displaying posts from yammer and show in our page and also the user can post from the website and it will be displayed on yammer website.

When I click "Sign in with Yammer" and it is authorized am I am getting access_token value. I saved that token in a session variable to use it for further data to retrieve.

I want to display posts for the signed in user in another button click event.

I use following endpoint to get the messages. But in webrequest I am getting 401 error (Unauthorized).What can be the reason. Please help.

Below is the code:

    public void getmessages()
    {
        string endPoint = "https://www.yammer.com/api/v1/messages.json?access_token="+Session["accesstoken"].ToString()+"";
        HttpWebRequest webrequest = WebRequest.Create(endPoint) as HttpWebRequest;
        webrequest.Method = "GET";
        string result = null;
        using (HttpWebResponse resp = webrequest.GetResponse()
                                      as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            result = reader.ReadToEnd();
        }
     }

Solution

  • Add yammer access token to Authorization Bearer to web request.

    HttpWebRequest webrequest = WebRequest.Create(endPoint) as HttpWebRequest;
    webrequest.Headers.Add("Authorization", "Bearer" + " " + Session["accesstoken"].ToString());