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 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; | |
// DotNetOpenAuth用の名前空間 | |
using DotNetOpenAuth.Messaging; | |
using DotNetOpenAuth.OAuth; | |
using DotNetOpenAuth.OAuth.ChannelElements; | |
// DynamicJSON用の名前空間 | |
using Codeplex.Data; | |
namespace DotNetOpenAuthWithTwitter | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var timeline = new MessageReceivingEndpoint("https://api.twitter.com/1.1/statuses/home_timeline.json", | |
HttpDeliveryMethods.GetRequest); | |
using (var r = timeline.Get()) | |
using (var s = r.GetResponseReader()) | |
{ | |
var json = DynamicJson.Parse(s.ReadToEnd()); | |
foreach (var item in json) | |
{ | |
Console.WriteLine(item.text); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
public static 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 static IncomingWebResponse Get(this MessageReceivingEndpoint endpoint) | |
{ | |
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 } }); | |
var consumer = new DesktopConsumer(serviceProviderDescription, tokenManager); | |
return consumer.PrepareAuthorizedRequestAndSend(endpoint, AccessToken); | |
} | |
} | |
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