4

現在、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 プロジェクトの一部です (名前、twitter ユーザー名、プロフィール画像、フォロワー、ツイート数)。Twitter API がユーザーのメールアドレスを取得しないことは知っています。しかし、ユーザーのプロファイル ID とユーザーのプロファイル リンクを取得したいのですが...これら 2 つのデータを取得するには、上記のコードから何を変更する必要があるか教えてもらえますか?

完全なソース コードへのリンクは次のとおりです。

4

1 に答える 1

3

account/verify_credentials に使用しているコードは問題ないようです。それが機能している場合は、同じコードを使用できますが、URL を変更してください。account/verify_credentials は認証されたユーザーに対して機能しますが、任意のユーザーを指定するにはusers/showを使用する必要があります。アカウント/設定エンドポイントがありますが、これも認証済みユーザー専用です。

個々のユーザーのみが自分の設定ページを表示できます。ユーザーを自分のプロファイル ページであるhttps://twitter.com/settings/accountに送信できますが、表示するにはログインする必要があります。これがあなたの要件を満たすかどうかはわかりませんが、できることの 1 つは、次のように個人の公開 Twitter ページを識別することです。

string publicTwitterPage = "https://twitter.com/" + username;
于 2013-03-30T19:13:09.517 に答える