有没有人知道Tumblr的任何自定义OAuthClient?我正在浏览Tumblr api documentation,最后几乎没有关于如何创建一个与DotNetOpenAuth一起使用的线索。
答案 0 :(得分:1)
这样的事情可能有用:
Web Config
<add key="TumblrKey" value="x" />
<add key="TumblrSecret" value="x" />
令牌存储(来自/基于示例项目):
public class InMemoryTokenManager : IConsumerTokenManager
{
private readonly Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();
public InMemoryTokenManager(string consumerKey, string consumerSecret)
{
if(string.IsNullOrEmpty(consumerKey))
{
throw new ArgumentNullException("consumerKey");
}
ConsumerKey = consumerKey;
ConsumerSecret = consumerSecret;
}
public string ConsumerKey { get; private set; }
public string ConsumerSecret { get; private set; }
#region ITokenManager Members
public string GetTokenSecret(string token)
{
return tokensAndSecrets[token];
}
public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)
{
tokensAndSecrets[response.Token] = response.TokenSecret;
}
public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
{
tokensAndSecrets.Remove(requestToken);
tokensAndSecrets[accessToken] = accessTokenSecret;
}
public TokenType GetTokenType(string token)
{
throw new NotImplementedException();
}
#endregion
}
控制器
public class TumblrController : Controller
{
private const string AuthorizationUrl = "http://www.tumblr.com/oauth/authorize";
private const string AccessTokenUrl = "http://www.tumblr.com/oauth/access_token";
private const string RequestTokenUrl = "http://www.tumblr.com/oauth/request_token";
private static readonly ConcurrentDictionary<string, InMemoryTokenManager> TokenStore = new ConcurrentDictionary<string, InMemoryTokenManager>();
private static readonly WebConsumer TumblrConsumer;
static TumblrController()
{
TumblrConsumer = new WebConsumer(TumblrServiceProviderDescription, ShortTermUserSessionTokenManager);
}
public ActionResult Auth()
{
var callBack = new Uri("http://your.com/callback");
var request = TumblrConsumer.Channel.PrepareResponse(TumblrConsumer.PrepareRequestUserAuthorization(callBack, null, null));
request.Send();
return new EmptyResult();
}
public ActionResult Callback()
{
var auth = TumblrConsumer.ProcessUserAuthorization();
// do something with auth
return new EmptyResult();
}
private static ServiceProviderDescription TumblrServiceProviderDescription
{
get
{
var tumblrDescription = new ServiceProviderDescription
{
UserAuthorizationEndpoint = new MessageReceivingEndpoint(AuthorizationUrl, HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
AccessTokenEndpoint = new MessageReceivingEndpoint(AccessTokenUrl, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
RequestTokenEndpoint = new MessageReceivingEndpoint(RequestTokenUrl, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }
};
return tumblrDescription;
}
}
private static InMemoryTokenManager ShortTermUserSessionTokenManager
{
get
{
InMemoryTokenManager tokenManager;
TokenStore.TryGetValue("Tumblr", out tokenManager);
if (tokenManager == null)
{
tokenManager = new InMemoryTokenManager(ConfigurationManager.AppSettings["TumblrKey"], ConfigurationManager.AppSettings["TumblrSecret"]);
TokenStore.TryAdd("Tumblr", tokenManager);
}
return tokenManager;
}
}
}