使用Twitter登录.使用linq2twitter的OAuth或Xauth [英] Sign in using twitter. OAuth or Xauth using linq2twitter

查看:90
本文介绍了使用Twitter登录.使用linq2twitter的OAuth或Xauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在努力解决这个问题.目前,我有一个简单的c#控制台应用程序.最终,我想制作一个小型库,以便在移动应用程序中重用,以便通过Twitter登录,但这是以后要解决的问题.目前,我有以下代码,从理论上讲,应该允许我登录Twitter.

For the past few days, I am struggling with this issue. At this moment, I have a simple c# console app. Ultimately, I want to make a small library to be reused in mobile apps for sign in with twitter, but that is a problem for later. At this moment, I have the following code, that should in theory allow me to sign in to 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 ) 毕竟,这是一个测试应用程序,以了解它是如何完成的.

I have not yet sent a XAuth access request to twitter. (https://dev.twitter.com/docs/oauth/xauth) This is after all a test app to see how it's done.

我的问题是这个.我可以允许我的用户在不使用Xauth的情况下为Twitter提供其用户名和密码并登录吗?如果可能的话,我该怎么做?这是一个更好的解决方案吗?如果您能给我一些使用linq2twitter做到这一点的例子,我将非常满意.我是一名菜鸟开发人员,到处都是墙壁...同样,此处给定的代码,如果我从twitter获得Xauth访问权限,是否可以正常工作?

My Question is this. Can I allow my users to give their username and password for twitter and sign in without using Xauth ? How do I do this if it is possible... Is it a better solution ? If you can give me examples of how to do this using linq2twitter, I would be very great full. I am a rookie developer and I am running into walls everywhere... Also, the given code here, will this work if I got Xauth access from twitter ?

提前谢谢大家.我真的被困住了,谷歌现在开始讨厌我了……

Thank you all in advance. I am really stuck and google is starting to hate me by now...

编辑...

我找到了此链接 https://dev.twitter.com/docs/auth/implementing-sign-twitter 但是我得到了401未经授权的退货...不知道为什么,如果发现错误,请告诉我.我认为它是回调网址,但是我不太确定

I found this link https://dev.twitter.com/docs/auth/implementing-sign-twitter But I got a 401 Unauthorized return... dont know why, if you spot something wrong let me know. I think its the callback url, but I am a bit unsure

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.

I dont yet understand what to use for the callback url.

推荐答案

这是不可能的,您正在使用库(dlls ...)来访问Twitter,而Twitter则不允许您未经oAuth授权(至少我不知道其他方式).

This is not possible, you are using the library (dlls...), for the access on the twitter, and the Twitter does not allow that you be authorized without oAuth (at least I do not know any other way).

您在 TwitterDev 上创建的应用程序非常重要; D.我有与您现在相同的错误,但我认为!在TwitterDev中尝试以下代码:

And the Application that you make on TwitterDev is very important ;D. I have the same error that you have now, but I think!. Try the follow code in the 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行... 甚至更多

My errors are on the line 4,5,6 and 7... Even more

这篇关于使用Twitter登录.使用linq2twitter的OAuth或Xauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆