GET/POST errors from html5/winjs metro apps


#1

Hello,

Could some one help me out to post a tweet from js. As there is no SDK/Nu-get package. I have tried and after some work Im able to get token and all the stuff. I stuck bound at POSTwhere it always return me with 400 or 401 errors with messages Bad Authentication data,215 or Unable to authenticate you ,32.

Where as GET for https://api.twitter.com/1.1/users/show.json’ is working fine. Unable to cope up with post.

Below is my code snippet.

 function posttoTwitter() {
        //https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
        //https://dev.twitter.com/docs/api/1.1/post/statuses/update

        var status = encodeURIComponent("Explore show Case metro app from the store");

        var twitterURL = "https://api.twitter.com/1.1/statuses/update.json";
        var ClientID = "ZQBIILZI9PiA8aKG1RjNHw";
        var ClientSecret = "zKJ9l9qutrdIhCM7rVS7Rd5PPRfU3gCshJrMQ3s00";
        var callbackURL = "http://itshowcase.cloudapp.net/";

        // Acquiring a request token
        var timestamp = Math.round(new Date().getTime() / 1000.0);
        var nonce = Math.random();//Windows.Security.Cryptography.CryptographicBuffer.generateRandomNumber();// 
        nonce = Math.floor(nonce * 1000000000);

        // Compute base signature string and sign it.
        //    This is a common operation that is required for all requests even after the token is obtained.
        //    Parameters need to be sorted in alphabetical order
        //    Keys and values should be URL Encoded.

        var sigBaseStringParams = "oauth_callback=" + encodeURIComponent(callbackURL);
        sigBaseStringParams += "&" + "oauth_consumer_key=" + ClientID;
        sigBaseStringParams += "&" + "oauth_nonce=" + nonce;
        sigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1";
        sigBaseStringParams += "&" + "oauth_token=" + userAccessToken;
        sigBaseStringParams += "&" + "oauth_timestamp=" + timestamp;
        sigBaseStringParams += "&" + "oauth_version=1.0";
        sigBaseStringParams += "&" + "status=" + encodeURIComponent(status);

        var sigBaseString = "POST&";
        sigBaseString += encodeURIComponent(twitterURL) + "&" + encodeURIComponent(sigBaseStringParams);
        var keyText = encodeURIComponent(ClientSecret) + "&" + encodeURIComponent(userAccessTokenSecret);
        var keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(keyText, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
        var macAlgorithmProvider = Windows.Security.Cryptography.Core.MacAlgorithmProvider.openAlgorithm("HMAC_SHA1");
        var key = macAlgorithmProvider.createKey(keyMaterial);
        var tbs = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(sigBaseString, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
        var signatureBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, tbs);
        var signature = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(signatureBuffer);
        var dataToPost = "OAuth oauth_callback=" + encodeURIComponent(callbackURL) + "\",oauth_consumer_key=\"" + ClientID + "\", oauth_nonce=\"" + nonce + "\",oauth_signature=\"" + encodeURIComponent(signature) + "\",oauth_signature_method=\"HMAC-SHA1" + "\",oauth_timestamp=\"" + timestamp + "\",oauth_token=\"" + userAccessToken + "\", oauth_version=\"1.0\"";

        var response = sendTweetRequest(twitterURL, dataToPost, encodeURIComponent(status));



    }


    function sendTweetRequest(url, authzheader, requestbody) {
        try {

            var request = new XMLHttpRequest();
            request.open("POST", url, false);
            request.setRequestHeader("Authorization:", authzheader);


            request.send(requestbody);
            if (request.status != "200") {
                console.log(request);
            }
            return request.responseText;
        } catch (err) {
            WinJS.log("Error sending request: " + err, "Web Authentication SDK Sample", "error");
        }
    }

Tried a lot… could some one help me on this please.