1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
//-----------------------------------------------------------------------
// <copyright file="TwitterConsumer.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.ApplicationBlock {
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Bindings;
using DotNetOpenAuth.OAuth;
using DotNetOpenAuth.OAuth.ChannelElements;
using Newtonsoft.Json.Linq;
/// <summary>
/// A consumer capable of communicating with Twitter.
/// </summary>
public class TwitterConsumer : Consumer {
/// <summary>
/// The description of Twitter's OAuth protocol URIs for use with actually reading/writing
/// a user's private Twitter data.
/// </summary>
public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/authorize",
"https://api.twitter.com/oauth/access_token");
/// <summary>
/// The description of Twitter's OAuth protocol URIs for use with their "Sign in with Twitter" feature.
/// </summary>
public static readonly ServiceProviderDescription SignInWithTwitterServiceDescription = new ServiceProviderDescription(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/authenticate",
"https://api.twitter.com/oauth/access_token");
/// <summary>
/// The URI to get a user's favorites.
/// </summary>
private static readonly Uri GetFavoritesEndpoint = new Uri("http://twitter.com/favorites.xml");
/// <summary>
/// The URI to get the data on the user's home page.
/// </summary>
private static readonly Uri GetFriendTimelineStatusEndpoint = new Uri("https://api.twitter.com/1.1/statuses/home_timeline.json");
private static readonly Uri UpdateProfileBackgroundImageEndpoint = new Uri("http://twitter.com/account/update_profile_background_image.xml");
private static readonly Uri UpdateProfileImageEndpoint = new Uri("http://twitter.com/account/update_profile_image.xml");
private static readonly Uri VerifyCredentialsEndpoint = new Uri("http://api.twitter.com/1/account/verify_credentials.xml");
/// <summary>
/// Initializes a new instance of the <see cref="TwitterConsumer"/> class.
/// </summary>
public TwitterConsumer() {
this.ServiceProvider = ServiceDescription;
this.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
this.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
this.TemporaryCredentialStorage = HttpContext.Current != null
? (ITemporaryCredentialStorage)new CookieTemporaryCredentialStorage()
: new MemoryTemporaryCredentialStorage();
}
/// <summary>
/// Initializes a new instance of the <see cref="TwitterConsumer"/> class.
/// </summary>
/// <param name="consumerKey">The consumer key.</param>
/// <param name="consumerSecret">The consumer secret.</param>
public TwitterConsumer(string consumerKey, string consumerSecret)
: this() {
this.ConsumerKey = consumerKey;
this.ConsumerSecret = consumerSecret;
}
/// <summary>
/// Gets a value indicating whether the Twitter consumer key and secret are set in the web.config file.
/// </summary>
public static bool IsTwitterConsumerConfigured {
get {
return !string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerKey"]) &&
!string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerSecret"]);
}
}
public static Consumer CreateConsumer(bool forWeb = true) {
string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
string consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
if (IsTwitterConsumerConfigured) {
ITemporaryCredentialStorage storage = forWeb ? (ITemporaryCredentialStorage)new CookieTemporaryCredentialStorage() : new MemoryTemporaryCredentialStorage();
return new Consumer(consumerKey, consumerSecret, ServiceDescription, storage) {
HostFactories = new TwitterHostFactories(),
};
} else {
throw new InvalidOperationException("No Twitter OAuth consumer key and secret could be found in web.config AppSettings.");
}
}
/// <summary>
/// Prepares a redirect that will send the user to Twitter to sign in.
/// </summary>
/// <param name="forceNewLogin">if set to <c>true</c> the user will be required to re-enter their Twitter credentials even if already logged in to Twitter.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The redirect message.
/// </returns>
public static async Task<Uri> StartSignInWithTwitterAsync(bool forceNewLogin = false, CancellationToken cancellationToken = default(CancellationToken)) {
var redirectParameters = new Dictionary<string, string>();
if (forceNewLogin) {
redirectParameters["force_login"] = "true";
}
Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_");
var consumer = CreateConsumer();
consumer.ServiceProvider = SignInWithTwitterServiceDescription;
Uri redirectUrl = await consumer.RequestUserAuthorizationAsync(callback, cancellationToken: cancellationToken);
return redirectUrl;
}
/// <summary>
/// Checks the incoming web request to see if it carries a Twitter authentication response,
/// and provides the user's Twitter screen name and unique id if available.
/// </summary>
/// <param name="completeUrl">The URL that came back from the service provider to complete the authorization.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A tuple with the screen name and Twitter unique user ID if successful; otherwise <c>null</c>.
/// </returns>
public static async Task<Tuple<string, int>> TryFinishSignInWithTwitterAsync(Uri completeUrl = null, CancellationToken cancellationToken = default(CancellationToken)) {
var consumer = CreateConsumer();
consumer.ServiceProvider = SignInWithTwitterServiceDescription;
var response = await consumer.ProcessUserAuthorizationAsync(completeUrl ?? HttpContext.Current.Request.Url, cancellationToken: cancellationToken);
if (response == null) {
return null;
}
string screenName = response.ExtraData["screen_name"];
int userId = int.Parse(response.ExtraData["user_id"]);
return Tuple.Create(screenName, userId);
}
public async Task<JArray> GetUpdatesAsync(AccessToken accessToken, CancellationToken cancellationToken = default(CancellationToken)) {
if (string.IsNullOrEmpty(accessToken.Token)) {
throw new ArgumentNullException("accessToken.Token");
}
using (var httpClient = this.CreateHttpClient(accessToken)) {
using (var response = await httpClient.GetAsync(GetFriendTimelineStatusEndpoint, cancellationToken)) {
response.EnsureSuccessStatusCode();
string jsonString = await response.Content.ReadAsStringAsync();
var json = JArray.Parse(jsonString);
return json;
}
}
}
public async Task<XDocument> GetFavorites(AccessToken accessToken, CancellationToken cancellationToken = default(CancellationToken)) {
if (string.IsNullOrEmpty(accessToken.Token)) {
throw new ArgumentNullException("accessToken.Token");
}
using (var httpClient = this.CreateHttpClient(accessToken)) {
using (HttpResponseMessage response = await httpClient.GetAsync(GetFavoritesEndpoint, cancellationToken)) {
response.EnsureSuccessStatusCode();
return XDocument.Parse(await response.Content.ReadAsStringAsync());
}
}
}
public async Task<XDocument> UpdateProfileBackgroundImageAsync(AccessToken accessToken, string image, bool tile, CancellationToken cancellationToken) {
var imageAttachment = new StreamContent(File.OpenRead(image));
imageAttachment.Headers.ContentType = new MediaTypeHeaderValue("image/" + Path.GetExtension(image).Substring(1).ToLowerInvariant());
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, UpdateProfileBackgroundImageEndpoint);
var content = new MultipartFormDataContent();
content.Add(imageAttachment, "image");
content.Add(new StringContent(tile.ToString().ToLowerInvariant()), "tile");
request.Content = content;
request.Headers.ExpectContinue = false;
using (var httpClient = this.CreateHttpClient(accessToken)) {
using (HttpResponseMessage response = await httpClient.SendAsync(request, cancellationToken)) {
response.EnsureSuccessStatusCode();
string responseString = await response.Content.ReadAsStringAsync();
return XDocument.Parse(responseString);
}
}
}
public Task<XDocument> UpdateProfileImageAsync(AccessToken accessToken, string pathToImage, CancellationToken cancellationToken = default(CancellationToken)) {
string contentType = "image/" + Path.GetExtension(pathToImage).Substring(1).ToLowerInvariant();
return this.UpdateProfileImageAsync(accessToken, File.OpenRead(pathToImage), contentType, cancellationToken);
}
public async Task<XDocument> UpdateProfileImageAsync(AccessToken accessToken, Stream image, string contentType, CancellationToken cancellationToken = default(CancellationToken)) {
var imageAttachment = new StreamContent(image);
imageAttachment.Headers.ContentType = new MediaTypeHeaderValue(contentType);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, UpdateProfileImageEndpoint);
var content = new MultipartFormDataContent();
content.Add(imageAttachment, "image", "twitterPhoto");
request.Content = content;
using (var httpClient = this.CreateHttpClient(accessToken)) {
using (HttpResponseMessage response = await httpClient.SendAsync(request, cancellationToken)) {
response.EnsureSuccessStatusCode();
string responseString = await response.Content.ReadAsStringAsync();
return XDocument.Parse(responseString);
}
}
}
public async Task<XDocument> VerifyCredentialsAsync(AccessToken accessToken, CancellationToken cancellationToken = default(CancellationToken)) {
using (var httpClient = this.CreateHttpClient(accessToken)) {
using (var response = await httpClient.GetAsync(VerifyCredentialsEndpoint, cancellationToken)) {
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync()) {
return XDocument.Load(XmlReader.Create(stream));
}
}
}
}
public async Task<string> GetUsername(AccessToken accessToken, CancellationToken cancellationToken = default(CancellationToken)) {
XDocument xml = await this.VerifyCredentialsAsync(accessToken, cancellationToken);
XPathNavigator nav = xml.CreateNavigator();
return nav.SelectSingleNode("/user/screen_name").Value;
}
private class TwitterHostFactories : IHostFactories {
private static readonly IHostFactories underlyingFactories = new DefaultOAuthHostFactories();
public HttpMessageHandler CreateHttpMessageHandler() {
return new WebRequestHandler();
}
public HttpClient CreateHttpClient(HttpMessageHandler handler = null) {
var client = underlyingFactories.CreateHttpClient(handler);
// Twitter can't handle the Expect 100 Continue HTTP header.
client.DefaultRequestHeaders.ExpectContinue = false;
return client;
}
}
}
}
|