Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

POST request to Twitter with Netduino Plus 2

sockets c# twitter-api

  • Please log in to reply
1 reply to this topic

#1 ioexception

ioexception

    New Member

  • Members
  • Pip
  • 4 posts

Posted 20 December 2014 - 06:25 PM

string oauth_nonce = Utility.RandomString(32);
        string oauth_timestamp = Utility.Timestamp.ToString();
        string oauth_signature = null;
        string encodedTweet = HttpUtility.UrlEncode(tweet);
        string signingKey = HttpUtility.UrlEncode(ConsumerSecret) + "&" + HttpUtility.UrlEncode(AccessTokenSecret);
        string requestLine = "POST https://api.twitter.com/1.1/statuses/update.json HTTP/1.1";
        string requestBody = "status=" + encodedTweet;

        string paramsString = "oauth_consumer_key=" + HttpUtility.UrlEncode(ConsumerKey) +
                                "&oauth_nonce=" + HttpUtility.UrlEncode(oauth_nonce) +
                                "&oauth_signature_method=" + HttpUtility.UrlEncode("HMAC-SHA1") +
                                "&oauth_timestamp=" + oauth_timestamp +
                                "&oauth_token=" + HttpUtility.UrlEncode(AccessToken) +
                                "&oauth_version=" + HttpUtility.UrlEncode("1.0") +
                                "&" + requestBody;

        string signatureBase = "POST&" + HttpUtility.UrlEncode("https://api.twitter.com/1.1/statuses/update.json") +
                                "&" + HttpUtility.UrlEncode(paramsString);

        byte[] signatureBytes = Sha.ComputeHMACSHA1(Encoding.UTF8.GetBytes(signingKey),
            Encoding.UTF8.GetBytes(signatureBase));
        oauth_signature = HttpUtility.ConvertToBase64String(signatureBytes);

        byte[] requestBodyBytes = Encoding.UTF8.GetBytes(requestBody);

        string otherHeaders = "Host: api.twitter.com" + CRLF +
                                "Connection: Close" + CRLF +
                                "Accept: */*" + CRLF +
                                "User-Agent: Tuitduino v0.1" + CRLF +
                                "Content-Type: application/x-www-form-urlencoded" + CRLF +
                                "Content-Length: " + requestBodyBytes.Length;

        string authorization = "Authorization: OAuth oauth_consumer_key=\"" + HttpUtility.UrlEncode(ConsumerKey) + "\", " +
                                "oauth_nonce=\"" + HttpUtility.UrlEncode(oauth_nonce) + "\", " +
                                "oauth_signature=\"" + HttpUtility.UrlEncode(oauth_signature) + "\", " +
                                "oauth_signature_method=\"HMAC-SHA1\", " +
                                "oauth_timestamp=\"" + oauth_timestamp + "\", " +
                                "oauth_token=\"" + HttpUtility.UrlEncode(AccessToken) + "\", " +
                                "oauth_version=\"1.0\"";

        string fullRequest = requestLine + CRLF + otherHeaders + CRLF + authorization + CRLF + CRLF + requestBody;
        byte[] requestBytes = Encoding.UTF8.GetBytes(fullRequest);

        using (var socket = Network.Connect())
        {
            socket.Send(requestBytes, requestBytes.Length, 0);
        }

Hi guys, I'm new to netduino and I'm trying to send a POSTrequest to update an status to Twitter API, at this moment I generate the request and send it using raw sockets but it always return 403 Authentication Required, but when I grab the same request and send it va Fiddler everything goes OK. Can any of you tell me what is wrong? I'm thinking that it might has something to do with indicating the end of the body message.  Here is my code:



#2 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 13 October 2015 - 12:02 PM

I came across this article, which seems relevant to what you're trying to do:  http://mattisenhower...6/microtweet-2/

  • In the article he talks about how Twitter disabled their v1.0 API and started enforcing the use of SSL connections

See also comments he makes here:  http://mattisenhower...tduino-3-wi-fi/

  • He talks about how the Netduino 3 WiFi is "the first Netduino to support SSL out of the box"

I don't know what one would have to do to retrofit SSL to a Netduino Plus 2 or even if it's possible.  Perhaps other people can comment on that...

 


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.