Last active
August 29, 2015 14:04
-
-
Save thinkAmi/54b755f3cd8df4f11a98 to your computer and use it in GitHub Desktop.
Twitter Single-user Auth sample: Using DotNetOpenAuth and DynamicJson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Console Applicatoin | |
// Add library using NuGet | |
// - DotNetOpenAuth OAuth 1.0(a) Consumer (byAndrew Amott) | |
// - DynamicJson (by neuecc) | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
// DotNetOpenAuth用の名前空間 | |
using DotNetOpenAuth.Messaging; | |
using DotNetOpenAuth.OAuth; | |
using DotNetOpenAuth.OAuth.ChannelElements; | |
// DynamicJSON用の名前空間 | |
using Codeplex.Data; | |
namespace DotNetOpenAuthForTweetAndTimeline | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var consumer = new TwitterConsumer(); | |
// Twitter API: POST statuses/update の利用 | |
var postEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/1.1/statuses/update.json", | |
HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest); | |
var messages = new[] { DotNetOpenAuth.Messaging.MultipartPostPart.CreateFormPart("status", "テスト:" + DateTime.Now.ToString("O")) }; | |
var postRequest = consumer.Post(postEndpoint, messages); | |
using (var r = postRequest.GetResponse()) | |
using (var rs = r.GetResponseStream()) | |
using (var sr = new System.IO.StreamReader(rs)) | |
{ | |
var json = DynamicJson.Parse(sr.ReadToEnd()); | |
Console.WriteLine(json.text); | |
} | |
// Twitter API: GET statuses/home_timeline | |
var getEndpont = new MessageReceivingEndpoint("https://api.twitter.com/1.1/statuses/home_timeline.json", | |
HttpDeliveryMethods.GetRequest); | |
var getRequest = consumer.Get(getEndpont); | |
using (var r = getRequest.GetResponse()) | |
using (var rs = r.GetResponseStream()) | |
using (var sr = new System.IO.StreamReader(rs)) | |
{ | |
var json = DynamicJson.Parse(sr.ReadToEnd()); | |
foreach (var item in json) | |
{ | |
Console.WriteLine(item.text); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
public class TwitterConsumer | |
{ | |
// TwitterAPIのキー関連 | |
private const string ApiKey = "your API key"; | |
private const string ApiSecret = "your API Secret"; | |
private const string AccessToken = "your AccessToken"; | |
private const string AccessSecret = "your AccessSecret"; | |
public DesktopConsumer Consumer { get; private set; } | |
public TwitterConsumer() | |
{ | |
var serviceProviderDescription = new ServiceProviderDescription | |
{ | |
UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/authorize", HttpDeliveryMethods.GetRequest), | |
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }, | |
ProtocolVersion = ProtocolVersion.V10a, | |
}; | |
var tokenManager = new TwitterTokenManager(ApiKey, ApiSecret, new Dictionary<string, string>() { { AccessToken, AccessSecret } }); | |
Consumer = new DesktopConsumer(serviceProviderDescription, tokenManager); | |
} | |
public System.Net.WebRequest Get(MessageReceivingEndpoint endpoint) | |
{ | |
return Consumer.PrepareAuthorizedRequest(endpoint, AccessToken); | |
} | |
public System.Net.WebRequest Post(MessageReceivingEndpoint endpoint, IEnumerable<MultipartPostPart> messages) | |
{ | |
// PrepareAuthorizedRequestAndSend()メソッドではPOSTのメッセージを送信できなかったことから、 | |
// PrepareAuthorizedRequest()メソッドを使っている | |
return Consumer.PrepareAuthorizedRequest(endpoint, AccessToken, messages); | |
} | |
} | |
public class TwitterTokenManager : IConsumerTokenManager | |
{ | |
private Dictionary<string, string> _accessKey; | |
public TwitterTokenManager(string consumerKey, string consumerSecret, Dictionary<string, string> accessKey) | |
{ | |
ConsumerKey = consumerKey; | |
ConsumerSecret = consumerSecret; | |
_accessKey = accessKey; | |
} | |
// DotNetOpenAuth.OAuth.ChannelElements.IConsumerTokenManagerインタフェースの実装 | |
public string ConsumerKey { get; private set; } | |
public string ConsumerSecret { get; private set; } | |
// DotNetOpenAuth.OAuth.ChannelElements.ITokenManagerインタフェースの実装 | |
// TwitterのSingle-user Authでは、GetTokenSecret()メソッド以外を使わないので、例外を投げておく | |
public void ExpireRequestTokenAndStoreNewAccessToken( | |
string consumerKey, string requestToken, string accessToken, string accessTokenSecret) | |
{ | |
throw new NotImplementedException(); | |
} | |
public string GetTokenSecret(string token) | |
{ | |
return _accessKey[token]; | |
} | |
public TokenType GetTokenType(string token) | |
{ | |
throw new NotImplementedException(); | |
} | |
public void StoreNewRequestToken(DotNetOpenAuth.OAuth.Messages.UnauthorizedTokenRequest request, | |
DotNetOpenAuth.OAuth.Messages.ITokenSecretContainingMessage response) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment