Search code examples
javayahoo-api

Java:Yahoo contacts API


is there any library or at least some documentation or example on how to import Yahoo! contacts using java and OAuth ?

in my website i need to display/get the yahoo contacts (with oauth)

is there any example.


Solution

  • There is no client library. You can retrieve contacts in two steps:

    Step 1:

    Getting 'TOKEN' and 'TOKEN SECRET' of user , using OAuth1. Some libraries are scribe and signpost .

    Step 2:

    After retrieving these tokens you have to get the yahoo id of the user.

    Example: (I am using signpost for this)

        OAuthConsumer consumer = new DefaultOAuthConsumer('YOUR CLIENT ID', 'YOUR CLIENT SECRET');
        URL url = new URL("http://social.yahooapis.com/v1/me/guid?format=json");
        HttpURLConnection request1 = (HttpURLConnection) url.openConnection();
        consumer.setTokenWithSecret('TOKEN', 'TOKEN SECRET');
        consumer.sign(request1);
        request1.connect();
        String responseBody = convertStreamToString(request1.getInputStream());
    

    After this, you have to use the yahoo id of the user retrieved from the user, to get user contacts.

    Example:

        OAuthConsumer consumer = new DefaultOAuthConsumer('YOUR CLIENT ID', 'YOUR CLIENT SECRET');
        URL url = new URL("http://social.yahooapis.com/v1/user/YAHOO_USER_ID/contacts?format=json");
        HttpURLConnection request1 = (HttpURLConnection) url.openConnection();
        consumer.setTokenWithSecret('TOKEN', 'TOKEN SECRET');
        consumer.sign(request1);
        request1.connect();
        String responseBody = convertStreamToString(request1.getInputStream());
    

    Method for stream Conversion used above is:

        public static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
        } finally {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
        return sb.toString();
    }