问题
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);
});
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.
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...
EDIT...
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;
I dont yet understand what to use for the callback url.
回答1:
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).
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
My errors are on the line 4,5,6 and 7... Even more
来源:https://stackoverflow.com/questions/18396917/sign-in-using-twitter-oauth-or-xauth-using-linq2twitter