2013-08-23 30 views
0

在過去的幾天裏,我正在爲這個問題而苦惱。在這一刻,我有一個簡單的C#控制檯應用程序。最終,我想讓一個小型圖書館在移動應用程序中重複使用,以便使用twitter進行登錄,但這對於以後的工作是個問題。此時,我有以下代碼,理論上應允許我登錄到Twitter。使用推特登錄。 OAuth或Xauth使用linq2twitter

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) 這畢竟是一個測試應用程序,看看它是如何完成的。

我的問題是這樣的。我可以允許我的用戶提供他們的Twitter用戶名和密碼,並且無需使用Xauth登錄?如果可能的話,我該如何做到這一點......這是一個更好的解決方案嗎?如果你能給我舉例說明如何使用linq2twitter來做到這一點,我會非常滿意。我是一名菜鳥開發人員,而且我到處都是牆壁......此外,如果我從Twitter獲得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。

回答

1

這是不可能的,你正在使用圖書館(DLL ...),在Twitter上訪問,並且Twitter不允許你沒有oAuth的授權(至少我不知道任何其他方式)。

而你在TwitterDev上的應用程序非常重要; D。我現在有同樣的錯誤,但我想!嘗試後續代碼在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 ... 更