-
Notifications
You must be signed in to change notification settings - Fork 2
/
OAuthConsumer.cs
executable file
·129 lines (115 loc) · 5.15 KB
/
OAuthConsumer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
namespace OAuth
{
public class OAuthConsumer : OAuthBase
{
private OAuthConfig _oauthConfig;
public OAuthConfig OauthConfig
{
get { return _oauthConfig; }
set { _oauthConfig = value; }
}
public OAuthConsumer(OAuthConfig oauthConfig, string debugType) : base (debugType)
{
this._oauthConfig = oauthConfig;
}
private void _saveTokenDataInConfiguration(string tokens)
{
if (tokens.Length == 0) return;
string[] tokenData = tokens.Split('&');
string[] tokenValue;
foreach (string data in tokenData)
{
tokenValue = data.Split('=');
switch (tokenValue[0])
{
case "oauth_token":
this._oauthConfig.OauthToken = tokenValue[1];
break;
case "oauth_token_secret":
this._oauthConfig.OauthTokenSecret = tokenValue[1];
break;
case "oauth_token_access":
this._oauthConfig.OauthTokenSecret = tokenValue[1];
break;
case "xoauth_token_ttl":
this._oauthConfig.OauthTokenTtl = tokenValue[1];
break;
}
}
}
private void _openAuthorizationPage()
{
string url = this._oauthConfig.UserAuthorizationUrl;
string oauth_token = this._oauthConfig.OauthToken;
List<QueryParameter> parameters = new List<QueryParameter>();
parameters.Add(new QueryParameter("oauth_token", oauth_token));
url += "?oauth_token=" + oauth_token;
System.Diagnostics.Process.Start(url);
}
public string getRequestToken()
{
string oauth_token = this._oauthConfig.OauthToken;
OAuthRequest request = new OAuthRequest(this, base._debugType);
string tokens = request.request(new Uri(this._oauthConfig.RequestTokenUrl), "GET", oauth_token, "", null).ToString();
if (tokens == String.Empty || tokens.Length == 0) return null;
// Save Tokens in Configuration
this._saveTokenDataInConfiguration(tokens);
// Open Authorization Page
this._openAuthorizationPage();
return tokens;
}
public string getAccessToken(string oauth_verifier)
{
string oauth_token = this._oauthConfig.OauthToken;
string oauth_token_secret = this._oauthConfig.OauthTokenSecret;
List<QueryParameter> parameters = new List<QueryParameter>();
//parameters.Add(new QueryParameter("oauth_token", oauth_token));
parameters.Add(new QueryParameter("oauth_verifier", oauth_verifier));
OAuthRequest request = new OAuthRequest(this, base._debugType);
string tokens = request.request(new Uri(this._oauthConfig.AccessTokenUrl), "GET", oauth_token, oauth_token_secret, parameters).ToString();
if (tokens == String.Empty || tokens.Length == 0) return null;
// Save Tokens in Configuration
this._saveTokenDataInConfiguration(tokens);
return tokens;
}
public Object request(string url, string httpMethod, List<QueryParameter> parameters, string responseFormat)
{
string oauth_token = this._oauthConfig.OauthToken;
string oauth_token_secret = this._oauthConfig.OauthTokenSecret;
OAuthRequest request = new OAuthRequest(this, base._debugType);
string response = request.request(new Uri(url), httpMethod, oauth_token, oauth_token_secret, parameters).ToString();
if (response == String.Empty || response.Length == 0)
{
base._debug("The Request Response was empty");
return null;
}
Object result = null;
switch (responseFormat)
{
case "DataSet":
System.IO.StringReader strreader = new System.IO.StringReader(response);
DataSet ds = new DataSet();
ds.ReadXml(strreader);
result = ds;
break;
case "XML":
System.IO.StringReader strxmlreader = new System.IO.StringReader(response);
XmlTextReader xmlReader = new XmlTextReader(strxmlreader);
xmlReader.Read();
result = xmlReader;
break;
case "PLAIN":
default:
result = response;
break;
}
return result;
}
}
}