我目前正在使用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的完整源代碼。
看起來不錯,但我怎麼可以檢索用戶id號? – reaz
在現場指南中,他們爲響應中的每個字段定義。對於用戶對象,https://dev.twitter.com/docs/platform-objects/users,它是「id」。 –