在过去的几天里,我一直在努力解决这个问题。此刻,我有一个简单的 c# 控制台应用程序。最终,我想制作一个小型库,以便在移动应用程序中重用以使用 twitter 登录,但这是以后的问题。此刻,我有以下代码,理论上应该可以让我登录 Twitter。
var auth = new XAuthAuthorizer()
{
Credentials = new XAuthCredentials()
{
UserName = "username",
Password = "supersecretpassword",
ConsumerKey = "2131341234Q123123",
ConsumerSecret = "671723458671253481234"
}
};
auth.Authorize();
using (var twitterCtx = new TwitterContext(auth))
{
//Log
twitterCtx.Log = Console.Out;
var users =
(from tweet in twitterCtx.User
where tweet.Type == UserType.Search &&
tweet.ScreenName == ""
select tweet)
.ToList();
users.ForEach(user =>
{
var status =
user.Protected || user.Status == null ?
"Status Unavailable" :
user.Status.Text;
Console.WriteLine(
"ID: {0}, Name: {1}\nLast Tweet: {2}\n",
user.Identifier.UserID, user.Identifier.ScreenName, status);
});
我还没有向 twitter 发送 XAuth 访问请求。(https://dev.twitter.com/docs/oauth/xauth)这毕竟是一个测试应用程序,看看它是如何完成的。
我的问题是这个。我可以允许我的用户在不使用 Xauth 的情况下提供他们的 twitter 用户名和密码并登录吗?如果可能的话,我该怎么做......这是一个更好的解决方案吗?如果你能给我举个例子说明如何使用 linq2twitter 做到这一点,我会非常满意。我是一名新手开发人员,我到处碰壁……此外,这里给出的代码,如果我从 twitter 获得 Xauth 访问权限,这会起作用吗?
谢谢大家。我真的被困住了,谷歌现在开始讨厌我了......
编辑...
我找到了这个链接 https://dev.twitter.com/docs/auth/implementing-sign-twitter 但是我收到了 401 Unauthorized return ... 不知道为什么,如果你发现有什么问题,请告诉我。我认为它的回调网址,但我有点不确定
string oauth_signature_method = "HMAC-SHA1";
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
string oauth_timestamp = Convert.ToInt64(ts.TotalSeconds).ToString();
string oauth_version = "1.0";
string oauth_consumer_key = "123123412341235";
string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
sd.Add("oauth_version", oauth_version);
sd.Add("oauth_consumer_key", oauth_consumer_key);
sd.Add("oauth_nonce", oauth_nonce);
sd.Add("oauth_signature_method", oauth_signature_method);
sd.Add("oauth_timestamp", oauth_timestamp);
UrlEntity callback = new UrlEntity();
callback.Url = @"http://127.0.0.1";
string encodedCallbackUrl = HttpUtility.UrlEncode(callback.Url);
sd.Add("oauth_callback",encodedCallbackUrl);
WebClient wc = new WebClient();
wc.Headers.Add("User-Agent: randomAgent HTTP Client");
wc.Headers.Add("Host: api.twitter.com");
wc.Headers.Add(@"Accept: */*");
UrlEntity url = new UrlEntity();
url.Url = @"https://api.twitter.com/oauth/request_token";
string signature = CreateSignature(url, sd);
sd.Add("oauth_signature",signature);
string dataValues = "";
foreach (KeyValuePair<string, string> pair in sd)
{
dataValues += pair.Key + "='" + pair.Value + "',";
}
dataValues = dataValues.Substring(0, dataValues.Length - 1); // cuts off the last,
string headerVal = " Oauth " + dataValues;
wc.Headers.Add("Authorization",headerVal);
wc.UploadString(@"https://api.twitter.com/oauth/request_token", "");
wc.DownloadStringCompleted += WcOnDownloadStringCompleted;
我还不明白回调 url 使用什么。