1

I only know twitter user's screen_name and ID. I want to get twitter avatar image like that;

https://api.twitter.com/1/users/profile_image?screen_name=StackExchange&size=bigger

how can i do it with twitter api 1.1 ?

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
ASPMaker
  • 303
  • 5
  • 14
  • I just did this yesterday with c#, please see http://stackoverflow.com/questions/17067996/authenticate-and-request-a-users-timeline-with-twitter-api-1-1-oauth/17071447#17071447 upvote it if you find it useful. There is a GitHub project too. It returns a timeline but you can use it to begin with, I think the json has it in or use the specific call. – hutchonoid Jun 13 '13 at 12:28
  • it is very big code for that. i only want to avatar picture. thank you. – ASPMaker Jun 13 '13 at 13:28
  • I know, it is compared to the old method. You must authenticate now. The second call would be the only difference you would have to use this api call https://api.twitter.com/1.1/users/show.json?screen_name=rsarver Let me know if you want me to post an answer with the c# code and I can for you. – hutchonoid Jun 13 '13 at 13:35
  • ok if you want you can do it – ASPMaker Jun 13 '13 at 13:54
  • I've added an answer in c#, please upvote and accept if it is of any use. – hutchonoid Jun 13 '13 at 14:20
  • ok i up vote :/ but it is not usefull for this work, you must know that – ASPMaker Jun 13 '13 at 14:22
  • http://stackoverflow.com/questions/14836956/how-to-get-user-image-with-twitter-api-1-1 – Ciro Santilli OurBigBook.com May 18 '16 at 20:42

1 Answers1

2

Ok, here goes.

You will have to add your twitter issued key, secret and screenname.

This returns the json that you need, I pass this back to my application and use as I used to before the api change.

        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();
            }
        }
hutchonoid
  • 32,982
  • 15
  • 99
  • 104