OAuth failure, win 8 app


#1

Hello everyone, I’m new in using the twitter API and winrt development. I’m trying to get some tweets using the 1.1 api but for some reason my app fails to authenticate. I followed this tutorial - http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net and did some adjustments for the winrt. Thank you in advance.

Here is where the request is sent:

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.ExpectContinue = false;

        string content = "";
        foreach (var feed in feedsToGet)
        {
            TwitterAuth.ResourceURL = feed;
            httpClient.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(TwitterAuth.CreateGetAuthHeader(feed));
            try
            {
                HttpResponseMessage httpResult = await httpClient.GetAsync(feed);
                content = await httpResult.Content.ReadAsStringAsync();
            }
            catch (Exception)
            {
                result.Add(GetTweet("Tweets exceeded 150 per hour.", "Mars Updates", DateTime.Now.ToString(), ""));
                return result;
            }
            JsonArray jsonTweets = JsonArray.Parse(content);

And the code where the header is created:

static string GetTimeStamp()
{
return Convert.ToInt64((DateTime.UtcNow -
new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds).ToString();
}

    static string GetNonce()
    {
        return Convert.ToBase64String(new UTF8Encoding().GetBytes(DateTime.Now.Ticks.ToString()));
    }

    public static string ResourceURL
    {
        get;
        set;
    }

static string baseGetFormat = “oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}”; //&status={6}
//static string lala = “oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}”;
static string CreateGetBaseString(string timeStamp, string nonce)
{
var baseStr = string.Format(baseGetFormat, oauthConsumerKey, nonce, oauthSignatureMethod,
timeStamp, oauthToken, oauthVersion);
baseStr = string.Concat(“GET&”, Uri.EscapeDataString(ResourceURL), “&”, Uri.EscapeDataString(baseStr));
return baseStr;
}

    public static string CreateGetAuthHeader(string resource_url)
    {
        string timeStamp = GetTimeStamp();
        string nonce = GetNonce();

        string compositeKey = string.Concat(Uri.EscapeDataString(oauthConsumerSecret), 
            "&", Uri.EscapeDataString(oauthTokenSecret));

        string baseString = CreateGetBaseString(timeStamp, nonce);

        string oauth_signature = Sha1Encrypt(baseString, compositeKey);

        string headerFormat = "OAuth oauth_consumer_key=\"{3}\", oauth_nonce=\"{0}\", oauth_signature=\"{5}\", oauth_signature_method=\"{1}\", oauth_timestamp=\"{2}\", oauth_token=\"{4}\", oauth_version=\"{6}\"";
        string authHeader = string.Format(headerFormat,
                    Uri.EscapeDataString(nonce),
                    Uri.EscapeDataString(oauthSignatureMethod),
                    Uri.EscapeDataString(timeStamp),
                    Uri.EscapeDataString(oauthConsumerKey),
                    Uri.EscapeDataString(oauthToken),
                    Uri.EscapeDataString(oauth_signature),
                    Uri.EscapeDataString(oauthVersion)
            );
        return authHeader;
    }

    public static string Sha1Encrypt(string baseString, string keyString)
    {
        var crypt = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
        var buffer = CryptographicBuffer.ConvertStringToBinary(baseString, BinaryStringEncoding.Utf8);
        var keyBuffer = CryptographicBuffer.ConvertStringToBinary(keyString, BinaryStringEncoding.Utf8);
        var key = crypt.CreateKey(keyBuffer);

        var sigBuffer = CryptographicEngine.Sign(key, buffer);
        string signature = CryptographicBuffer.EncodeToBase64String(sigBuffer);
        return signature;
    }

#2

Did you get an answer? I am getting the same error.


#3

Mtabaana 25,000 thousand did not find them


#4

تابِع @uae5e1