forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterAuthProvider.cs
More file actions
52 lines (44 loc) · 1.94 KB
/
TwitterAuthProvider.cs
File metadata and controls
52 lines (44 loc) · 1.94 KB
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
using System;
using System.Collections.Generic;
using ServiceStack.Common;
using ServiceStack.Configuration;
using ServiceStack.Text;
namespace ServiceStack.ServiceInterface.Auth
{
public class TwitterAuthProvider : OAuthProvider
{
public const string Name = "twitter";
public static string Realm = "https://api.twitter.com/";
public TwitterAuthProvider(IResourceManager appSettings)
: base(appSettings, Realm, Name) {}
protected override void LoadUserAuthInfo(AuthUserSession userSession, IOAuthTokens tokens, Dictionary<string, string> authInfo)
{
if (authInfo.ContainsKey("user_id"))
tokens.UserId = authInfo.GetValueOrDefault("user_id");
if (authInfo.ContainsKey("screen_name"))
tokens.UserName = authInfo.GetValueOrDefault("screen_name");
try
{
if (tokens.UserId != null)
{
var json = AuthHttpGateway.DownloadTwitterUserInfo(tokens.UserId);
var obj = JsonObject.Parse(json);
tokens.DisplayName = obj.Get("name");
}
LoadUserOAuthProvider(userSession, tokens);
}
catch (Exception ex)
{
Log.Error("Could not retrieve twitter user info for '{0}'".Fmt(userSession.TwitterUserId), ex);
}
}
public override void LoadUserOAuthProvider(IAuthSession authSession, IOAuthTokens tokens)
{
var userSession = authSession as AuthUserSession;
if (userSession == null) return;
userSession.TwitterUserId = tokens.UserId ?? userSession.TwitterUserId;
userSession.TwitterScreenName = tokens.UserName ?? userSession.TwitterScreenName;
userSession.DisplayName = tokens.DisplayName ?? userSession.DisplayName;
}
}
}