class Program
{
string apiKey = "";
string apiKeySecret = "";
string requestToken = "";
string requestTokenSecret = "";
string accessToken = "";
string accessTokenSecret = "";
Uri requestTokenUri = new Uri("http://api.justin.tv/oauth/request_token");
Uri accessTokenUri = new Uri("http://api.justin.tv/oauth/access_token");
string authorizationUri = "http://api.justin.tv/oauth/authorize";
OAuthBase oAuth = new OAuthBase();
private Dictionary<string, string> parseResponse(string parameters)
{
Dictionary<string, string> result = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(parameters))
{
string[] p = parameters.Split('&');
foreach (string s in p)
if (!string.IsNullOrEmpty(s))
if (s.IndexOf('=') > -1)
{
string[] temp = s.Split('=');
result.Add(temp[0], temp[1]);
}
else result.Add(s, string.Empty);
}
return result;
}
//1. 获取Request Token,该步骤使用API Key和API Key Secret签名
public void getRequestToken()
{
Uri uri = requestTokenUri;
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizeUrl, normalizedRequestParameters;
// 签名
string sig = oAuth.GenerateSignature(
uri,
apiKey,
apiKeySecret,
string.Empty,
string.Empty,
"GET",
timeStamp,
nonce,
OAuthBase.SignatureTypes.HMACSHA1,
out normalizeUrl,
out normalizedRequestParameters);
sig = HttpUtility.UrlEncode(sig);
//构造请求Request Token的url
StringBuilder sb = new StringBuilder(uri.ToString());
sb.AppendFormat("?oauth_consumer_key={0}&", apiKey);
sb.AppendFormat("oauth_nonce={0}&", nonce);
sb.AppendFormat("oauth_timestamp={0}&", timeStamp);
sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
sb.AppendFormat("oauth_version={0}&", "1.0");
sb.AppendFormat("oauth_signature={0}", sig);
Console.WriteLine("请求Request Token的url: \n" + sb.ToString());
//请求Request Token
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string responseBody = stream.ReadToEnd();
stream.Close();
response.Close();
Console.WriteLine("请求Request Token的返回值: \n" + responseBody);
//解析返回的Request Token和Request Token Secret
Dictionary<string, string> responseValues = parseResponse(responseBody);
requestToken = responseValues["oauth_token"];
requestTokenSecret = responseValues["oauth_token_secret"];
}
// 2. 用户确认授权
public void authorization()
{
//生成引导用户授权的url
string url = authorizationUri + "?oauth_token=" + requestToken;
Console.WriteLine("请将下面url粘贴到浏览器中,并同意授权,同意后按任意键继续:");
Console.WriteLine(url);
}
// 3. 换取Access Token,该步骤使用API Key、API Key Secret、Request Token和Request Token Secret签名
public void getAccessToken()
{
Uri uri = accessTokenUri;
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizeUrl, normalizedRequestParameters;
// 签名
string sig = oAuth.GenerateSignature(
uri,
apiKey,
apiKeySecret,
requestToken,
requestTokenSecret,
"GET",
timeStamp,
nonce,
OAuthBase.SignatureTypes.HMACSHA1,
out normalizeUrl,
out normalizedRequestParameters);
sig = HttpUtility.UrlEncode(sig);
//构造请求Access Token的url
StringBuilder sb = new StringBuilder(uri.ToString());
sb.AppendFormat("?oauth_consumer_key={0}&", apiKey);
sb.AppendFormat("oauth_nonce={0}&", nonce);
sb.AppendFormat("oauth_timestamp={0}&", timeStamp);
sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
sb.AppendFormat("oauth_version={0}&", "1.0");
sb.AppendFormat("oauth_signature={0}&", sig);
sb.AppendFormat("oauth_token={0}&", requestToken);
Console.WriteLine("请求Access Token的url: \n" + sb.ToString());
//请求Access Token
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string responseBody = stream.ReadToEnd();
stream.Close();
response.Close();
Console.WriteLine("请求Access Token的返回值: \n" + responseBody);
//解析返回的Request Token和Request Token Secret
Dictionary<string, string> responseValues = parseResponse(responseBody);
accessToken = responseValues["oauth_token"];
accessTokenSecret = responseValues["oauth_token_secret"];
}
static void Main(string[] args)
{
try
{
Program p = new Program();
p.getRequestToken();
p.authorization();
p.getAccessToken();
}
catch (System.Exception ex)
{
Console.Write("Bad Request!! \n");
}
System.Console.ReadLine();
}
}