C# 使用twitter登录。使用linq2twitter的OAuth或Xauth

C# 使用twitter登录。使用linq2twitter的OAuth或Xauth,c#,linq-to-twitter,C#,Linq To Twitter,在过去的几天里,我一直在努力解决这个问题。现在,我有一个简单的c#控制台应用程序。最终,我想制作一个小程序库,在移动应用程序中重复使用,以便使用twitter登录,但这对以后来说是个问题。目前,我有以下代码,理论上应该允许我登录twitter var auth = new XAuthAuthorizer() { Credentials = new XAuthCredentials()

在过去的几天里,我一直在努力解决这个问题。现在,我有一个简单的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访问请求。() 这毕竟是一个测试应用,看看它是如何做到的

我的问题是。我可以允许我的用户在不使用Xauth的情况下为twitter提供用户名和密码并登录吗?如果可能的话,我该怎么做。。。这是更好的解决方案吗?如果你能给我举个例子,说明如何使用linq2twitter实现这一点,我会非常满意。我是一个新手开发人员,我到处碰壁。。。还有,这里给出的代码,如果我从twitter获得Xauth访问权限,这会起作用吗

提前谢谢大家。我真的被卡住了,谷歌现在开始恨我了

编辑

我找到了这个链接 但我得到了401未经授权的回报。。。不知道为什么,如果你发现什么不对劲,请告诉我。我想这是回拨url,但我有点不确定

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;
string oauth\u签名\u method=“HMAC-SHA1”;
TimeSpan ts=DateTime.UtcNow—新的日期时间(1970,1,1,0,0,0,DateTimeKind.Utc);
字符串oauth_timestamp=Convert.ToInt64(ts.TotalSeconds.ToString();
字符串oauth_version=“1.0”;
字符串oauth_consumer_key=“123123412341235”;
string oauth_nonce=Convert.ToBase64String(new ascienceoding().GetBytes(DateTime.Now.Ticks.ToString());
SortedDictionary sd=新的SortedDictionary();
添加(“oauth_版本”,oauth_版本);
添加(“oauth_消费者_密钥”,oauth_消费者_密钥);
sd.添加(“oauth_nonce”,oauth_nonce);
添加(“oauth_签名方法”,oauth_签名方法);
添加(“oauth_时间戳”,oauth_时间戳);
UrlEntity回调=新的UrlEntity();
callback.Url=@“http://127.0.0.1";
字符串encodedCallbackUrl=HttpUtility.UrlEncode(callback.Url);
Add(“oauth_回调”,encodedCallbackUrl);
WebClient wc=新的WebClient();
Add(“用户代理:随机代理HTTP客户端”);
Add(“主机:api.twitter.com”);
wc.Headers.Add(@“接受:*/”);
UrlEntity url=新的UrlEntity();
url.url=@“https://api.twitter.com/oauth/request_token";
字符串签名=CreateSignature(url,sd);
sd.添加(“oauth_签名”,签名);
字符串dataValues=“”;
foreach(sd中的KeyValuePair对)
{
dataValues+=pair.Key+“=”+pair.Value+“,”;
}
dataValues=dataValues.Substring(0,dataValues.Length-1);//剪掉最后一个,,
字符串headerVal=“Oauth”+数据值;
wc.Headers.添加(“授权”,headerVal);
wc.UploadString(@)https://api.twitter.com/oauth/request_token", "");
wc.DownloadStringCompleted+=WcOnDownloadStringCompleted;

我还不知道如何使用回调url。

这是不可能的,您正在使用库(DLL…)访问twitter,twitter不允许您在没有oAuth的情况下获得授权(至少我不知道其他方式)

你的申请非常重要;我犯的错误和你现在犯的一样,但我认为!。在TwitterDev中尝试以下代码:

1 -Access level       Read, write, and direct messages 
2 -Consumer key       your_consumer_key
3 -Consumer secret    your_very_large_consumer_secret_key
4 -Request token URL  https://api.twitter.com/oauth/request_token
5 -Authorize URL      https://api.twitter.com/oauth/authorize
6 -Access token URL   https://api.twitter.com/oauth/access_token
7 -Callback URL       https://api.twitter.com/oauth/authorize
Sign in with Twitter    Yes 
我的错误在第4、5、6和7行。。。 更