我怎么能得到与api 1.1推特头像
本文关键字:api 怎么能 | 更新日期: 2023-09-27 18:18:06
我只知道twitter用户的screen_name和ID。我想要得到这样的twitter头像;
https://api.twitter.com/1/users/profile_image?screen_name=StackExchange&大小=大
如何使用twitter API 1.1 ?
好了。
您必须添加您的twitter发布密钥,秘密和屏幕名。
这将返回你需要的json,我将其传递回我的应用程序,并像api更改之前一样使用。
var oAuthConsumerKey = "key";
var oAuthConsumerSecret = "secret";
var oAuthUrl = "https://api.twitter.com/oauth2/token";
var screenname = "StackExchange";
// Do the Authenticate
var authHeaderFormat = "Basic {0}";
var authHeader = string.Format(authHeaderFormat,
Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
Uri.EscapeDataString((oAuthConsumerSecret)))
));
var postBody = "grant_type=client_credentials";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "POST";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (Stream stream = authRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
stream.Write(content, 0, content.Length);
}
authRequest.Headers.Add("Accept-Encoding", "gzip");
WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
TwitAuthenticateResponse twitAuthResponse;
using (authResponse)
{
using (var reader = new StreamReader(authResponse.GetResponseStream())) {
JavaScriptSerializer js = new JavaScriptSerializer();
var objectText = reader.ReadToEnd();
twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
}
}
// Do the avatar
var avatarFormat =
"https://api.twitter.com/1.1/users/show.json?screen_name={0}";
var avatarUrl = string.Format(avatarFormat, screenname);
HttpWebRequest avatarRequest = (HttpWebRequest)WebRequest.Create(avatarUrl);
var timelineHeaderFormat = "{0} {1}";
avatarRequest.Headers.Add("Authorization",
string.Format(timelineHeaderFormat, twitAuthResponse.token_type,
twitAuthResponse.access_token));
avatarRequest.Method = "Get";
WebResponse timeLineResponse = avatarRequest.GetResponse();
var avatarJson = string.Empty;
using (authResponse)
{
using (var reader = new StreamReader(timeLineResponse.GetResponseStream()))
{
avatarJson = reader.ReadToEnd();
}
}