| 1 |
|
| 2 |
using System; |
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Security.Cryptography; |
| 5 |
using System.Configuration; |
| 6 |
using System.Text; |
| 7 |
using System.Web; |
| 8 |
using System.Net; |
| 9 |
using System.IO; |
| 10 |
|
| 11 |
namespace Twitter |
| 12 |
{ |
| 13 |
class Auth |
| 14 |
{ |
| 15 |
const string REQUEST_TOKEN_URL = "https://twitter.com/oauth/request_token"; |
| 16 |
const string ACCESS_TOKEN_URL = "https://twitter.com/oauth/access_token"; |
| 17 |
const string AUTHORIZE_URL = "https://twitter.com/oauth/authorize"; |
| 18 |
|
| 19 |
private Random random = new Random(); |
| 20 |
|
| 21 |
public string ConsumerKey { get; private set; } |
| 22 |
public string ConsumerSecret { get; private set; } |
| 23 |
public string RequestToken { get; private set; } |
| 24 |
public string RequestTokenSecret { get; private set; } |
| 25 |
public string AccessToken { get; private set; } |
| 26 |
public string AccessTokenSecret { get; private set; } |
| 27 |
public string UserId { get; private set; } |
| 28 |
public string ScreenName { get; private set; } |
| 29 |
|
| 30 |
public Auth(string consumerKey, string consumerSecret) |
| 31 |
{ |
| 32 |
ServicePointManager.Expect100Continue = false; |
| 33 |
ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12; |
| 34 |
ConsumerKey = consumerKey; |
| 35 |
ConsumerSecret = consumerSecret; |
| 36 |
} |
| 37 |
|
| 38 |
public Auth(string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret, string userId, string screenName) |
| 39 |
{ |
| 40 |
ServicePointManager.Expect100Continue = false; |
| 41 |
ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12; |
| 42 |
ConsumerKey = consumerKey; |
| 43 |
ConsumerSecret = consumerSecret; |
| 44 |
AccessToken = accessToken; |
| 45 |
AccessTokenSecret = accessTokenSecret; |
| 46 |
UserId = userId; |
| 47 |
ScreenName = screenName; |
| 48 |
} |
| 49 |
|
| 50 |
public void GetRequestToken() |
| 51 |
{ |
| 52 |
SortedDictionary<string, string> parameters = GenerateParameters(""); |
| 53 |
string signature = GenerateSignature("", "GET", REQUEST_TOKEN_URL, parameters); |
| 54 |
parameters.Add("oauth_signature", UrlEncode(signature)); |
| 55 |
string response = HttpGet(REQUEST_TOKEN_URL, parameters); |
| 56 |
Dictionary<string, string> dic = ParseResponse(response); |
| 57 |
RequestToken = dic["oauth_token"]; |
| 58 |
RequestTokenSecret = dic["oauth_token_secret"]; |
| 59 |
} |
| 60 |
|
| 61 |
public string GetAuthorizeUrl() |
| 62 |
{ |
| 63 |
return AUTHORIZE_URL + "?oauth_token=" + RequestToken; |
| 64 |
} |
| 65 |
|
| 66 |
public void GetAccessToken(string pin) |
| 67 |
{ |
| 68 |
SortedDictionary<string, string> parameters = GenerateParameters(RequestToken); |
| 69 |
parameters.Add("oauth_verifier", pin); |
| 70 |
string signature = GenerateSignature(RequestTokenSecret, "GET", ACCESS_TOKEN_URL, parameters); |
| 71 |
parameters.Add("oauth_signature", UrlEncode(signature)); |
| 72 |
string response = HttpGet(ACCESS_TOKEN_URL, parameters); |
| 73 |
Dictionary<string, string> dic = ParseResponse(response); |
| 74 |
AccessToken = dic["oauth_token"]; |
| 75 |
AccessTokenSecret = dic["oauth_token_secret"]; |
| 76 |
UserId = dic["user_id"]; |
| 77 |
ScreenName = dic["screen_name"]; |
| 78 |
} |
| 79 |
|
| 80 |
public string Get(string url, IDictionary<string, string> parameters) |
| 81 |
{ |
| 82 |
SortedDictionary<string, string> parameters2 = GenerateParameters(AccessToken); |
| 83 |
foreach (var p in parameters) |
| 84 |
parameters2.Add(p.Key, p.Value); |
| 85 |
string signature = GenerateSignature(AccessTokenSecret, "GET", url, parameters2); |
| 86 |
parameters2.Add("oauth_signature", UrlEncode(signature)); |
| 87 |
return HttpGet(url, parameters2); |
| 88 |
} |
| 89 |
|
| 90 |
public string Post(string url, IDictionary<string, string> parameters) |
| 91 |
{ |
| 92 |
SortedDictionary<string, string> parameters2 = GenerateParameters(AccessToken); |
| 93 |
foreach (var p in parameters) |
| 94 |
parameters2.Add(p.Key, p.Value); |
| 95 |
string signature = GenerateSignature(AccessTokenSecret, "POST", url, parameters2); |
| 96 |
parameters2.Add("oauth_signature", UrlEncode(signature)); |
| 97 |
return HttpPost(url, parameters2); |
| 98 |
} |
| 99 |
|
| 100 |
private string HttpGet(string url, IDictionary<string, string> parameters) |
| 101 |
{ |
| 102 |
WebRequest req = WebRequest.Create(url + '?' + JoinParameters(parameters)); |
| 103 |
WebResponse res = req.GetResponse(); |
| 104 |
Stream stream = res.GetResponseStream(); |
| 105 |
StreamReader reader = new StreamReader(stream); |
| 106 |
string result = reader.ReadToEnd(); |
| 107 |
reader.Close(); |
| 108 |
stream.Close(); |
| 109 |
return result; |
| 110 |
} |
| 111 |
|
| 112 |
string HttpPost(string url, IDictionary<string, string> parameters) |
| 113 |
{ |
| 114 |
byte[] data = Encoding.ASCII.GetBytes(JoinParameters(parameters)); |
| 115 |
WebRequest req = WebRequest.Create(url); |
| 116 |
req.Method = "POST"; |
| 117 |
req.ContentType = "application/x-www-form-urlencoded"; |
| 118 |
req.ContentLength = data.Length; |
| 119 |
Stream reqStream = req.GetRequestStream(); |
| 120 |
reqStream.Write(data, 0, data.Length); |
| 121 |
reqStream.Close(); |
| 122 |
WebResponse res = req.GetResponse(); |
| 123 |
Stream resStream = res.GetResponseStream(); |
| 124 |
StreamReader reader = new StreamReader(resStream, Encoding.UTF8); |
| 125 |
string result = reader.ReadToEnd(); |
| 126 |
reader.Close(); |
| 127 |
resStream.Close(); |
| 128 |
return result; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
private Dictionary<string, string> ParseResponse(string response) |
| 133 |
{ |
| 134 |
Dictionary<string, string> result = new Dictionary<string, string>(); |
| 135 |
foreach (string s in response.Split('&')) |
| 136 |
{ |
| 137 |
int index = s.IndexOf('='); |
| 138 |
if (index == -1) |
| 139 |
result.Add(s, ""); |
| 140 |
else |
| 141 |
result.Add(s.Substring(0, index), s.Substring(index + 1)); |
| 142 |
} |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
private string JoinParameters(IDictionary<string, string> parameters) |
| 147 |
{ |
| 148 |
StringBuilder result = new StringBuilder(); |
| 149 |
bool first = true; |
| 150 |
foreach (var parameter in parameters) |
| 151 |
{ |
| 152 |
if (first) |
| 153 |
first = false; |
| 154 |
else |
| 155 |
result.Append('&'); |
| 156 |
result.Append(parameter.Key); |
| 157 |
result.Append('='); |
| 158 |
result.Append(parameter.Value); |
| 159 |
} |
| 160 |
return result.ToString(); |
| 161 |
} |
| 162 |
|
| 163 |
private string GenerateSignature(string tokenSecret, string httpMethod, string url, SortedDictionary<string, string> parameters) |
| 164 |
{ |
| 165 |
string signatureBase = GenerateSignatureBase(httpMethod, url, parameters); |
| 166 |
HMACSHA1 hmacsha1 = new HMACSHA1(); |
| 167 |
hmacsha1.Key = Encoding.ASCII.GetBytes(UrlEncode(ConsumerSecret) + '&' + UrlEncode(tokenSecret)); |
| 168 |
byte[] data = System.Text.Encoding.ASCII.GetBytes(signatureBase); |
| 169 |
byte[] hash = hmacsha1.ComputeHash(data); |
| 170 |
return Convert.ToBase64String(hash); |
| 171 |
} |
| 172 |
|
| 173 |
private string GenerateSignatureBase(string httpMethod, string url, SortedDictionary<string, string> parameters) |
| 174 |
{ |
| 175 |
StringBuilder result = new StringBuilder(); |
| 176 |
result.Append(httpMethod); |
| 177 |
result.Append('&'); |
| 178 |
result.Append(UrlEncode(url)); |
| 179 |
result.Append('&'); |
| 180 |
result.Append(UrlEncode(JoinParameters(parameters))); |
| 181 |
return result.ToString(); |
| 182 |
} |
| 183 |
|
| 184 |
private SortedDictionary<string, string> GenerateParameters(string token) |
| 185 |
{ |
| 186 |
SortedDictionary<string, string> result = new SortedDictionary<string, string>(); |
| 187 |
result.Add("oauth_consumer_key", ConsumerKey); |
| 188 |
result.Add("oauth_signature_method", "HMAC-SHA1"); |
| 189 |
result.Add("oauth_timestamp", GenerateTimestamp()); |
| 190 |
result.Add("oauth_nonce", GenerateNonce()); |
| 191 |
result.Add("oauth_version", "1.0"); |
| 192 |
if (!string.IsNullOrEmpty(token)) |
| 193 |
result.Add("oauth_token", token); |
| 194 |
return result; |
| 195 |
} |
| 196 |
|
| 197 |
public string UrlEncode(string value) |
| 198 |
{ |
| 199 |
string unreserved = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"; |
| 200 |
StringBuilder result = new StringBuilder(); |
| 201 |
byte[] data = Encoding.UTF8.GetBytes(value); |
| 202 |
foreach (byte b in data) |
| 203 |
{ |
| 204 |
if (b < 0x80 && unreserved.IndexOf((char)b) != -1) |
| 205 |
result.Append((char)b); |
| 206 |
else |
| 207 |
result.Append('%' + String.Format("{0:X2}", (int)b)); |
| 208 |
} |
| 209 |
return result.ToString(); |
| 210 |
} |
| 211 |
|
| 212 |
private string GenerateNonce() |
| 213 |
{ |
| 214 |
string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 215 |
StringBuilder result = new StringBuilder(8); |
| 216 |
for (int i = 0; i < 8; ++i) |
| 217 |
result.Append(letters[random.Next(letters.Length)]); |
| 218 |
return result.ToString(); |
| 219 |
} |
| 220 |
|
| 221 |
private string GenerateTimestamp() |
| 222 |
{ |
| 223 |
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); |
| 224 |
return Convert.ToInt64(ts.TotalSeconds).ToString(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|