2013-03-29 99 views
4

我目前正在使用twitter API進行項目工作。如何使用asp.net獲取twitter API上的用戶信息

我有這樣的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Newtonsoft.Json.Linq; // Added for JSON Library (Doc) 
using System.Xml; 
using oAuthExample; 
public partial class twitterInfo : System.Web.UI.Page 
{ 
    string url = ""; 
    string xml = ""; 
    public string name = ""; 
    public string username = ""; 
    public string profileImage = ""; 
    public string followersCount = ""; 
    public string noOfTweets = ""; 
    public string recentTweet = ""; 

    //Source http://www.aspdotnet-suresh.com/2012/05/add-twitter-login-authentication-to.html 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     GetUserDetailsFromTwitter(); 
    } 
    private void GetUserDetailsFromTwitter() 
    { 
     if (Request["oauth_token"] != null & Request["oauth_verifier"] != null) 
     { 
      imgTwitter.Visible = false; 
      tbleTwitInfo.Visible = true; 
      var oAuth = new oAuthTwitter(); 
      //Get the access token and secret. 
      oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]); 
      if (oAuth.TokenSecret.Length > 0) 
      { 
       url = "https://api.twitter.com/1.1/account/verify_credentials.json"; 
       xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty); 

      JObject o = JObject.Parse(xml); 
      name = Convert.ToString(o["name"]); 
      username = Convert.ToString(o["screen_name"]); 
      profileImage = Convert.ToString(o["profile_image_url"]); 
      followersCount = Convert.ToString(o["followers_count"]); 
      noOfTweets = Convert.ToString(o["statuses_count"]); 
      noOfTweets = Convert.ToString(o["birthday"]); 

      } 
     } 
    } 
    protected void imgTwitter_Click(object sender, ImageClickEventArgs e) 
    { 
     var oAuth = new oAuthTwitter(); 
     if (Request["oauth_token"] == null) 
     { 
      //Redirect the user to Twitter for authorization. 
      //Using oauth_callback for local testing. 

         // R_ use the dynamic url director 
      // Call back URL to direct the user to the page 
      oAuth.CallBackUrl = "http://localhost:518/Account/TwitterInfo.aspx"; 

      Response.Redirect(oAuth.AuthorizationLinkGet()); 
     } 
     else 
     { 
      GetUserDetailsFromTwitter(); 
     } 
    } 
} 

此代碼是一個Twitter API的項目並返回(姓名,twitterusername,profileimage,追隨者和鳴叫的次數)的一部分。 我知道Twitter API不會檢索用戶的電子郵件地址。 但我想檢索用戶的配置文件ID和用戶的個人資料鏈接... 任何人都可以告訴我,我應該從上面的代碼更改來檢索這兩個數據?

這是a link的完整源代碼。

回答

3

您用於account/verify_credentials的代碼看起來沒問題,如果可行,您可以使用相同的代碼,但更改URL。 account/verify_credentials將對經過身份驗證的用戶有效,但您必須使用users/show來指定任何用戶。有一個account/settings終結點​​,但這只是爲經過身份驗證的用戶。

只有個人用戶才能看到他們自己的設置頁面。您可以將用戶發送到https://twitter.com/settings/account,這是他們自己的配置文件頁面,但他們需要登錄才能看到它。我不知道這是否會滿足你的要求,但你可以做的一件事是確定個人的公共Twitter頁面,如下所示:

string publicTwitterPage = "https://twitter.com/" + username; 
+0

看起來不錯,但我怎麼可以檢索用戶id號? – reaz

+1

在現場指南中,他們爲響應中的每個字段定義。對於用戶對象,https://dev.twitter.com/docs/platform-objects/users,它是「id」。 –

相關問題