Calling the Yahoo! Mail Web Service

Yahoo! Mail uses the Yahoo! Mail Web Service (YMWS) to list and summarize emails in your inbox folders, flag messages as read, retrieve a particular email for display, send email, and more. Your app can call the YMWS, too.

Many applications can get what they want out of the YMWS using the ymail.* YQL tables: ymail.folders, ymail.messages, ymail.msgcontent, and ymail.search. Apps that need access to data not exposed in YQL can use AuthService.callWebService to invoke the lower-level YMWS API directly.

OAuth Scopes and AuthService Setup

Yahoo! Mail Web Service (YMWS) calls are protected using OAuth. Your app can only access YMWS methods allowed by the scopes associated with its OAuth Consumer key. Configuring your app for accessing an endpoint that supports OAuth is explained in detail here.

There are two different ways to configure your scopes and get an AuthService object prepared to call web services:

Self Service

Obtain a key and secret from developer.yahoo.com. This is completely self-service, which is nice, but you have to do some work to orchestrate user authorization before you have an AuthService instance prepared to do web service calls.

Refined for Yahoo! Endpoints

Have a Yahoo! Mail Development Platform administrator configure your app to use OAuth refinements for Yahoo! endpoints. In this case, the keys and secrets (and scopes) are all managed by our server -- you don't need anything in your auth.xml. The user experience is stream-lined as well. It is easy to upgrade from the self-service approach to this one, so we don't normally enable Yahoo! endpoint refinements until your app is nearly ready for production.

If we do enable this support for your app, all you have to do to have an AuthService instance ready to call your webservice is this:

  1.  
  2. var auth = openmail.Application.getAuthService({});
 
   var auth = openmail.Application.getAuthService({});

YQL Examples

Assuming you've got an AuthService instance auth with the mail read-summary scope and that you've prepared it to make webservice calls in one of these ways, you're ready to talk to YQL. You could query YQL for the logged-on user's folder names using this code:

  1. auth.callWebService({
  2. url: 'http://query.yahooapis.com/v1/yql',
  3. method: 'GET',
  4. parameters: {
  5. format: "json",
  6. q: 'select * from ymail.folders'
  7. },
  8. function(args) {
  9. console.log("got data: ", args.data);
  10. }
  11. });
auth.callWebService({
    url: 'http://query.yahooapis.com/v1/yql',
    method: 'GET',
    parameters: {
        format: "json",
        q: 'select * from ymail.folders'
    },
    function(args) {
        console.log("got data: ", args.data);
    }
}); 

GetUserData Example

This code uses AuthService.callWebService to call the Yahoo! Mail Webservice GetUserData method.

  1.  
  2. //Invoked when the webservice responds
  3. function onResults(args){
  4. console.log('got ws result', args);
  5. }
  6.  
  7. var auth = openmail.Application.getAuthService({}),
  8. url = 'http://mail.yahooapis.com/ws/mail/v1.1/jsonrpc',
  9. params = [{}],
  10. method = 'GetUserData';
  11.  
  12. auth.callWebService({
  13. url: url,
  14. method:"POST",
  15. headers: [ 'content-type:application/json' ],
  16. parameters: {},
  17. body: YAHOO.lang.JSON.stringify({
  18. method: method,
  19. params: params,
  20. id: "123456789"
  21. })
  22. },
  23. onResults);
  24.  
 
//Invoked when the webservice responds
function onResults(args){
    console.log('got ws result', args);
}
 
var auth = openmail.Application.getAuthService({}),
    url = 'http://mail.yahooapis.com/ws/mail/v1.1/jsonrpc',
    params = [{}],
    method = 'GetUserData';
 
auth.callWebService({
        url: url,
        method:"POST",
        headers: [ 'content-type:application/json' ],
        parameters: {},
        body: YAHOO.lang.JSON.stringify({
               method: method,
               params: params,
               id: "123456789"
        })
    },
    onResults);
 
Notice the use of the headers and body attributes of the AuthService.callWebService service parameter.

ListMessages Example

This code uses the YMWS ListMessages method to list the first ten messages in the user's "Draft" folder:

  1.  
  2. //Invoked when the webservice responds
  3. function onResults(args){
  4. console.log('got ws result', args);
  5. }
  6.  
  7. var auth = openmail.Application.getAuthService({}),
  8. url = 'http://mail.yahooapis.com/ws/mail/v1.1/jsonrpc',
  9. params = [{fid:'Draft', numMid:10}],
  10. method = 'ListMessages';
  11.  
  12. auth.callWebService(
  13. {
  14. url: url,
  15. method:"POST",
  16. headers: [ 'content-type:application/json' ],
  17. parameters: {},
  18. body: YAHOO.lang.JSON.stringify({
  19. method: method,
  20. params: params,
  21. id: "123456789"
  22. })
  23. },
  24. onResults);
  25. }
  26.  
 
//Invoked when the webservice responds
function onResults(args){
    console.log('got ws result', args);
}
 
var auth = openmail.Application.getAuthService({}),
    url = 'http://mail.yahooapis.com/ws/mail/v1.1/jsonrpc',
    params = [{fid:'Draft', numMid:10}],
    method = 'ListMessages';
 
        auth.callWebService(
          {
            url: url,
            method:"POST",
            headers: [ 'content-type:application/json' ],
            parameters: {},
            body: YAHOO.lang.JSON.stringify({
                    method: method,
                    params: params,
                    id: "123456789"
                })
          },
          onResults);
}
 

Calling from Your Server

If your application includes a server-side component that you host, you have the option of making YMWS calls directly from your server instead of from the javascript running in the browser. In this case, you sign up on YDN for an "app key" (unrelated to your Yahoo! Mail app) for your server and access YMWS as documented here.

Be sure to use the JSON client and not the SOAP client. The SOAP client is getting deprecated.

Support & Community

Ask questions and share insights on our forum.