简体   繁体   中英

How can I grab a users profile image using the twitter api (js)?

Is there an easy way to grab a users twitter image with a simple line such as:

http://www.twitter.com/USERID/picture

If there isn't, what could I possibly look into that has an easy method to grab the user profile image?

UPDATE February 2014: The original answer no longer works now v1 of Twitters API has been retired. The below is the nearest substitute for what was asked, but for v1.1 of the API.


Version 1 of the Twitter API allowed you to do this easily, using a URL such as http://api.twitter.com/1/users/profile_image/:screen_name.format however that was retired in May 2013.

As of Verson 1.1, you cannot get a users profile picture URL without making an authenticated call . Once you have got an authenticated user, simply make a request to any endpoint which returns a users information ( such as /user/show ), and use the "profile_image_url";

jQuery.ajax('/users/show', {
    data: {
        screen_name: 'mattlunn'
    },
    headers: {
        // All your OAUTH headers
    }
}).done(function (res) {
    var url = res.profile_image_url;
    var img = document.createElement("img");

    alert("URL is '" + url + "'");

    img.src = url;
    document.body.appendChild(img);   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM