The remote server returned an error: (401) Unauthorized

oauth

#1

Hi I´m trying to get the banner of a public profile but I just keep getting this message: The remote server returned an error: (401) Unauthorized.

Can anyone check my code please and tell me what i´m doing wrong?

//Código fuente
public ActionResult GetNBanner()
{
//Construccion del header de la petición
string sURL = “https://api.twitter.com/1.1/users/profile_banner.json”;

//String oauthNonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture)));
String oauthNonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
String oauth_signature_method = “HMAC-SHA1”;
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
String oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
String oauth_version = “1.0”;
String HTTPMethod = “GET”;

//Obtener la firma para la petición
String oauth_signature = Getsignature(customer_key, oauthNonce, oauth_signature_method, oauth_timestamp, access_token, oauth_version, HTTPMethod, sURL);
String HeaderRequest = GetHeader(customer_key, oauthNonce, oauth_signature, oauth_signature_method,oauth_timestamp, access_token, oauth_version);

        WebRequest wrGETURL;
        wrGETURL = (HttpWebRequest)WebRequest.Create(sURL);
        wrGETURL.Method = HTTPMethod;
        wrGETURL.Timeout = 12000;
        wrGETURL.ContentType = "application/x-www-form-urlencoded";
        //wrGETURL.ContentType = "application/json";
        wrGETURL.Headers.Add("Authorization", HeaderRequest);

        Stream objStream;
        objStream = wrGETURL.GetResponse().GetResponseStream();

        StreamReader objReader = new StreamReader(objStream);

        string sLine = "";
        String resultado = string.Empty;
        int i = 0;

        while (sLine != null)
        {
            i++;
            sLine = objReader.ReadLine();
            if (sLine != null)
                resultado = resultado + sLine;
        }

        return Json(resultado, JsonRequestBehavior.AllowGet);

    }

    public String GetHeader(String soauth_consumer_key
                            , String soauth_nonce
                            , String soauth_signature
                            , String soauth_signature_method
                            , String soauth_timestamp
                            , String soauth_token
                            , String soauth_version) {
        //PASO 1 Obtener Parametros
        SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
        sd.Add("oauth_consumer_key", soauth_consumer_key);
        sd.Add("oauth_nonce", soauth_nonce);
        sd.Add("oauth_signature", soauth_signature);
        sd.Add("oauth_signature_method", soauth_signature_method);
        sd.Add("oauth_timestamp", soauth_timestamp);
        sd.Add("oauth_token", soauth_token);
        sd.Add("oauth_version", soauth_version);

        //PASO 2: Cadena del Header
        String DST = String.Empty;
        DST = "OAuth ";

        foreach (KeyValuePair<string, string> entry in sd)
        {
            DST += Uri.EscapeDataString(entry.Key) + '=' + '"' + Uri.EscapeDataString(entry.Value) +  '"' + ", ";
        }

        DST = DST.Remove(DST.Length - 2);

        return DST; 

    }

    public string Getsignature(
        String soauth_consumer_key
        , String soauth_nonce
        , String soauth_signature_method
        , String soauth_timestamp
        , String soauth_token
        , String soauth_version
        , String sHTTPMethod
        , String sBaseUrl)
    {
        //PASO 1: OBTENER PARAMETROS
        SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
        sd.Add(Uri.EscapeDataString("include_entities"), Uri.EscapeDataString("true"));
        sd.Add(Uri.EscapeDataString("oauth_consumer_key"), Uri.EscapeDataString(soauth_consumer_key));
        sd.Add(Uri.EscapeDataString("oauth_nonce"), Uri.EscapeDataString(soauth_nonce));
        sd.Add(Uri.EscapeDataString("oauth_signature_method"), Uri.EscapeDataString(soauth_signature_method));
        sd.Add(Uri.EscapeDataString("oauth_timestamp"), Uri.EscapeDataString(soauth_timestamp));
        sd.Add(Uri.EscapeDataString("oauth_token"), Uri.EscapeDataString(soauth_token));
        sd.Add(Uri.EscapeDataString("oauth_version"), Uri.EscapeDataString(soauth_version));

        //PASO 2: GENERAR UNA CADENA CON LOS PARAMETROS CODIFICADOS
        String ParameterString = String.Empty;

        foreach (KeyValuePair<string, string> entry in sd)
        {
            ParameterString += entry.Key + "=" + entry.Value + "&";
        }

        ParameterString = ParameterString.Remove(ParameterString.Length - 1);

        //PASO 3: GENERAR UNA CADENA BASE PARA EL SIGNATURE VALUE
        String SignatureBaseString = String.Empty;

        SignatureBaseString = sHTTPMethod.ToUpper() + "&" + Uri.EscapeDataString(sBaseUrl) + "&" + Uri.EscapeDataString(ParameterString);

        //PASO 4: Generar una llave compuesta
        String compositeKey = Uri.EscapeDataString(customer_key_secret) + "&" + Uri.EscapeDataString(access_token_secret);

        //PASO 5: Calcular la firma
        HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(compositeKey));
        string oauthSignature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(SignatureBaseString)));

        return oauthSignature;

    }