Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java:Yahoo contacts API

Tags:

java

yahoo-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.

like image 245
Sumit D Avatar asked Aug 01 '26 10:08

Sumit D


1 Answers

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();
}
like image 60
Jhanvi Avatar answered Aug 04 '26 01:08

Jhanvi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!