So Twitter killed OAuth 1.0 and broke some twitter devices I had, so tonight I thought I’d have a brief go at getting an app sending tweets again. You would think that Given the impending explosion of IoT devices (or so they say) that Twitter would like a chunk of that action, but I’m thinking maybe not. So the object here is to be able to create a device that can send a tweet directly. This likely involves OAuth and to that means I threw a Spider on a board with a J11 Ethernet module and a button and this is the code that I have thus far. NOTE its not working in that the data return looks pretty much empty.
NOTE to get the HTTPS working I used the FEZConfig tool and clicking on the ‘Deployment (Advanced)’ I clicked on ‘Update SSL Seed’, so it appears they are talking about apparently not about the right stuff.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.Net;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Time;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace tweetTest1
{
public partial class Program
{
private static readonly TimeServiceSettings tsSettings = new TimeServiceSettings();
// This method is run when the mainboard is powered up or reset.
private void ProgramStarted()
{
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
button.ButtonPressed += button_ButtonPressed;
ethernet_J11D.Interface.CableConnectivityChanged += Interface_CableConnectivityChanged;
ethernet_J11D.Interface.NetworkAddressChanged += Interface_NetworkAddressChanged;
ethernet_J11D.Interface.Open();
if (NetworkInterfaceExtension.AssignedNetworkInterface == null)
{
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface == null");
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
}
else
{
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface.IsActivated " +
NetworkInterfaceExtension.AssignedNetworkInterface.IsActivated);
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface != null");
if (NetworkInterfaceExtension.AssignedNetworkInterface.NetworkInterface.NetworkInterfaceType !=
NetworkInterfaceType.Ethernet)
{
Debug.Print(
"NetworkInterfaceExtension.AssignedNetworkInterface.NetworkInterface.NetworkInterfaceType != Microsoft.SPOT.Net.NetworkInformation.NetworkInterfaceType.Ethernet");
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
}
}
ethernet_J11D.Interface.NetworkInterface.EnableDhcp();
var tsSettings = new TimeServiceSettings();
FixedTimeService.Settings = new TimeServiceSettings();
FixedTimeService.SystemTimeChanged += FixedTimeService_SystemTimeChanged;
FixedTimeService.TimeSyncFailed += FixedTimeService_TimeSyncFailed;
InitTimeservice();
}
private void button_ButtonPressed(Button sender, Button.ButtonState state)
{
GetOauth();
}
private void InitTimeservice()
{
Thread.Sleep(1); // let ProgramStarted finish before doing this
FixedTimeService.Settings = new TimeServiceSettings(); // wait for internet connection
while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
Thread.Sleep(500);
// configure TimeServiceSettings
tsSettings.AutoDayLightSavings = true;
tsSettings.ForceSyncAtWakeUp = true;
// "nist1-sj.ustiming.org,us.pool.ntp.org,clock.tricity.wsu.edu,clock-1.cs.cmu.edu,time-a.nist.gov"
tsSettings.PrimaryServer = Dns.GetHostEntry("nist1-sj.ustiming.org").AddressList[0].GetAddressBytes();
tsSettings.AlternateServer = Dns.GetHostEntry("pool.ntp.org").AddressList[0].GetAddressBytes();
tsSettings.RefreshTime = 24*60*60; // once a day
// configure & start FixedTimeService
FixedTimeService.Settings = tsSettings;
FixedTimeService.SetTimeZoneOffset(-7*60); /// PST
FixedTimeService.SetDst("Mar Sun>=8 @ 2", "Nov Sun>=1 @ 2", 60); // US DST
FixedTimeService.Start();
}
private void FixedTimeService_TimeSyncFailed(object sender, TimeSyncFailedEventArgs e)
{
throw new NotImplementedException();
}
private void FixedTimeService_SystemTimeChanged(object sender, SystemTimeChangedEventArgs e)
{
Debug.Print("network time received. Current Date Time is " + DateTime.Now);
}
private void Interface_NetworkAddressChanged(object sender, EventArgs e)
{
Debug.Print("Interface_NetworkAddressChanged");
Debug.Print("New address for the Network Interface ");
Debug.Print("Is DhCp enabled: " + ethernet_J11D.Interface.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + ethernet_J11D.Interface.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + ethernet_J11D.Interface.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + ethernet_J11D.Interface.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + ethernet_J11D.Interface.NetworkInterface.SubnetMask);
Debug.Print("Default Gateway: " + ethernet_J11D.Interface.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + ethernet_J11D.Interface.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < ethernet_J11D.Interface.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i + ":" + ethernet_J11D.Interface.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
}
private void Interface_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
{
Debug.Print("Cable Connectivity Changed");
}
private void GetOauth()
{
string oauth_consumer_key = "your_consumer_key";
string oauth_consumer_secret = "your_consumer_secret";
//Token URL
string oauth_url = "https://api.twitter.com/oauth2/token";
string authHeader = "Basic " +
Convert.ToBase64String(
Encoding.UTF8.GetBytes(oauth_consumer_key + ":" + oauth_consumer_secret));
var request = (HttpWebRequest) WebRequest.Create(oauth_url);
request.Headers.Add("Authorization", authHeader);
request.Headers.Add("Accept-Encoding", "gzip");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
try
{
byte[] postBody = Encoding.UTF8.GetBytes("grant_type=client_credentials");
request.ContentLength = postBody.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postBody, 0, postBody.Length);
stream.Close();
}
WebResponse response = request.GetResponse();
if (response != null)
{
long size = response.ContentLength;
var respData = new byte[size];
using (Stream stream = response.GetResponseStream())
{
stream.Read(respData, 0, respData.Length);
stream.Close();
var responseString = new string(Encoding.UTF8.GetChars(respData));
Debug.Print("Response was: " + responseString);
}
}
}
catch (Exception e)
{
Debug.Print("Exception in HttpWebRequest.GetResponse(): " + e);
}
}
}
}