지난 며칠 동안이 문제로 고심하고 있습니다. 지금은 간단한 C# 콘솔 앱이 있습니다. 궁극적으로, 트위터로 로그인하기 위해 모바일 앱에서 재사용 할 수있는 작은 라이브러리를 만들고 싶지만 나중에 문제가 될 수 있습니다. 이 순간, 나는 이론적으로 내가 트위터에 로그인 할 수 있도록해야한다는 다음 코드를 가지고있다.트위터를 사용하여 로그인하십시오. linq2twitter를 사용하는 OAuth 또는 Xauth
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);
});
아직 트위터에 XAuth 액세스 요청을 보내지 않았습니다. (https://dev.twitter.com/docs/oauth/xauth) 이것은 어떻게 완료되었는지를 보여주는 모든 테스트 응용 프로그램입니다.
내 질문은 여기 있습니다. 내 사용자가 xauth를 사용하지 않고 트위터에 대한 사용자 이름과 암호를 제공하고 로그인하도록 허용 할 수 있습니까? 가능한 경우 어떻게해야합니까? 더 나은 솔루션입니까? linq2twitter를 사용하여이 작업을 수행하는 방법에 대한 예제를 제공 할 수 있다면 매우 좋을 것입니다. 나는 신인 개발자이고 모든 곳의 벽에 뛰어 들고 있습니다 ... 또한 주어진 코드는 트위터에서 Xauth에 액세스하면이 코드가 작동합니까?
감사합니다. 왜 경우, 알고 난 정말 붙어이고 구글은
내가이 링크 https://dev.twitter.com/docs/auth/implementing-sign-twitter 을 발견하지만 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;
아직 콜백 URL에 사용할 것을 모른다.