Intentando obtener información de perfil de Twitter en c #

Intento recuperar los datos de perfil de un usuario autenticado de Twitter utilizando la home_timeline Llamada API. Tengo un método específico usado para este propósito y en este punto estoy tratando de devolver la cadena JSON para verificar que funcione, pero obtengo un error de 400.

public string getProfile(){
           //set your own keys and screen name
            var oauth_consumer_key = "consumerKeyHere";
            var oAuthConsumerSecret = "consumerSecretHere";
            var oAuthUrl = "https://api.twitter.com/1.1/statuses/home_timeline.json";
            var oauth_token_secret = "tokenSecretHere";
           //Authenticate
            var authHeaderFormat = "Basic {0}";

            var postBody = "grant_type=client_credentials";
            var oauth_nonce = Convert.ToBase64String(
                new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                           - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
            var oauth_token = token.access_token;//this token is generated by another class (token) where I retrieve the access token. Verified that authentication works.

           //oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

           //create oauth signature
            var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                         "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}";

            var baseString = string.Format(baseFormat,
                oauth_consumer_key,
                oauth_nonce,
                oauth_signature_method,
                oauth_timestamp,
                oauth_token,
                oauth_version
            );

            baseString = string.Concat("GET&", Uri.EscapeDataString("https://api.twitter.com/1.1/statuses/home_timeline.json"), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_key),
                "&", Uri.EscapeDataString(oauth_token_secret));

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))  {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

           //create the request header
            var headerFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " +
                               "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
                               "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                Uri.EscapeDataString(oauth_nonce),
                Uri.EscapeDataString(oauth_signature_method),
                Uri.EscapeDataString(oauth_timestamp),
                Uri.EscapeDataString(oauth_consumer_key),
                Uri.EscapeDataString(oauth_token),
                Uri.EscapeDataString(oauth_signature),
                Uri.EscapeDataString(oauth_version)
            );

            HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
            authRequest.Headers.Add("Authorization", authHeader);
            authRequest.Method = "GET";
            authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            WebResponse authResponse = authRequest.GetResponse();
           //deserialize into an object
            using (authResponse)
            {
                using (var reader = new StreamReader(authResponse.GetResponseStream())) {
                    String JS = reader.ReadToEnd();
                    return js;
                }
            }
        }
0

1 Respuestas

Mis argumentos estaban en el orden incorrecto para authHeader:

var headerFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " +
                               "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
                               "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                Uri.EscapeDataString(oauth_nonce),
                Uri.EscapeDataString(oauth_signature_method),
                Uri.EscapeDataString(oauth_timestamp),
                Uri.EscapeDataString(oauth_consumer_key),
                Uri.EscapeDataString(oauth_token),
                Uri.EscapeDataString(oauth_signature),
                Uri.EscapeDataString(oauth_version)
            );
0
agregado
Tiene oauth_token = \ "{5} \" y oauth_signature como el 5 ° parámetro. ¿Cómo podría eso alguna vez funcionar?
agregado el autor ProfK, fuente
Oh, a menos que estés mostrando cómo tus argumentos estaban en el orden equivocado. Mi error.
agregado el autor ProfK, fuente