locked
Facing Issue while POST with Twitter

    Question

  • 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 I'm able to get token and all the stuff. I stuck bound at POST where 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.

    Thanks & Regards Tejaswi Chandrapatla


    • Edited by Teja510 Wednesday, October 09, 2013 3:01 PM
    Wednesday, October 09, 2013 1:54 PM

Answers

  • Hi Teja510,

    I do not have experience work with twitter API, but I found a interesting thing for you: http://code.msdn.microsoft.com/windowsapps/WinJS-OAuth-for-Twitter-6fb22e37, Take a look to see if this can help you.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 10, 2013 1:35 AM
    Moderator
  • Thanks James.

    Some how I resolved the issue and working fine.


    Thanks & Regards Tejaswi Chandrapatla

    Thursday, October 10, 2013 7:01 AM

All replies

  • Hi Teja510,

    I do not have experience work with twitter API, but I found a interesting thing for you: http://code.msdn.microsoft.com/windowsapps/WinJS-OAuth-for-Twitter-6fb22e37, Take a look to see if this can help you.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 10, 2013 1:35 AM
    Moderator
  • Thanks James.

    Some how I resolved the issue and working fine.


    Thanks & Regards Tejaswi Chandrapatla

    Thursday, October 10, 2013 7:01 AM
  • Hi Teja510,

    If you read the last line of the page I provided, Version 1.3 adds support for POSTing data to Twitter and the link will direct you to a github page where you could find the code that support POST.

    BTW, congratulation that you solve the issue.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 10, 2013 7:31 AM
    Moderator
  • For just the authentication part of Twitter, the Web Authentication Broker includes OAuth and OAuth2 "filters" for Windows.Web.Http HttpClient; once configured for specific site (like Twitter), these filters will handle the auth aspects of talking to the twitter web service.

    Network Developer Experience Team (Microsoft)

    Friday, October 18, 2013 11:11 PM