httpyahooyahoo-apiyahoo-oauth

Http error 406 when I try to access yahoo API


I use yahoo oAuth1 and yahoo API to get contact list from yahoo server.

Here the code that I use to get contacts:

private void RetriveContacts()
        {
            OAuthBase oauth = new OAuthBase();
            Uri uri = new Uri("https://social.yahooapis.com/v1/user/" + (string)Session["xoauth_yahoo_guid"] + "/contacts?format=XML");
            string nonce = oauth.GenerateNonce();
            string timeStamp = oauth.GenerateTimeStamp();
            string normalizedUrl;
            string normalizedRequestParameters;
            string sig = oauth.GenerateSignature(uri, ConsumerKey, ConsumerSecret,
                                                 (string)Session["oauth_token"], (string)Session["oauth_token_secret"], "GET",
                                                 timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1,
                                                 out normalizedUrl, out normalizedRequestParameters);
        StringBuilder sbGetContacts = new StringBuilder(uri.ToString());

        try
        {
            string returnStr = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sbGetContacts.ToString());
            req.Method = "GET";

            string authHeader = "Authorization: OAuth " +
            "realm=\"yahooapis.com\"" +
            ",oauth_consumer_key=\"" + ConsumerKey + "\"" +
            ",oauth_nonce=\"" + nonce + "\"" +
            ",oauth_signature_method=\"HMAC-SHA1\"" +
            ",oauth_timestamp=\"" + timeStamp + "\"" +
            ",oauth_token=\"" + (string)Session["oauth_token"] + "\"" +
            ",oauth_version=\"1.0\"" +
            ",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";

            req.Headers.Add(authHeader);

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader streamReader = new StreamReader(res.GetResponseStream());
            returnStr = streamReader.ReadToEnd();

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(returnStr);
            XmlNodeList elemList = xmldoc.DocumentElement.GetElementsByTagName("fields");

            List<string> emails = new List<string>();
            for (int i = 0; i < elemList.Count; i++)
            {
                if (elemList[i].ChildNodes[1].InnerText == "email")
                    emails.Add(elemList[i].ChildNodes[2].InnerText);
                //Response.Write(elemList[i].ChildNodes[2].InnerText + "<br/>");
            }

        }
        catch (WebException ex)
        {
            //Response.Write(ex.Message);
            Response.Write("<br/>" + ex.Message + "</br>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            Response.Write("<br/>length: " + ex.Source.Length.ToString());
            Response.Write("<br/>stack trace: " + ex.StackTrace);
            Response.Write("<br/>status: " + ex.Status.ToString());
            HttpWebResponse res = (HttpWebResponse)ex.Response;
            int code = Convert.ToInt32(res.StatusCode);

            Response.Write("<br/>Status Code: (" + code.ToString() + ") " + res.StatusCode.ToString());
            Response.Write("<br/>Status Description: " + res.StatusDescription);

            if (ex.InnerException != null)
            {
                Response.Write("<br/>innerexception: " + ex.InnerException.Message);
            }

            if (ex.Source.Length > 0)
                Response.Write("<br/>source: " + ex.Source.ToString());

            if (res != null)
            {
                for (int i = 0; i < res.Headers.Count; i++)
                {
                    Response.Write("<br/>headers: " + i.ToString() + ": " + res.Headers[i]);
                }
            }
        }
    }

But in this row:

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

I get this Error:

enter image description here

Any why I get this error and how to fix it?

Thank you in advance.


Solution

  • Try setting the Accept header in your HTTP request. Something like this:

    Accept: text/html,*/*;q=0.9

    HTTP 406 indicates that either this header is expected but missing, or that it was present but did not specify a Content-Type that is compatible with the resource (e.g. asking for Accept: text/html when requesting a JPEG image.)

    See also the Accept-Charset, Accept-Encoding, and Accept-Language headers which can also trigger this status code.