Get Yahoo Contact List in C# application -


as guidance on https://developer.yahoo.com/oauth/guide/oauth-requesttoken.html, below did oauth yahoo contact list:

  1. make request https://api.login.yahoo.com/oauth/v2/get_request_token oauth_token , oauth_token_secret, them.

example url:

oauthbase oath = new oauthbase();  string url = "https://api.login.yahoo.com/oauth/v2/get_request_token?" +            "oauth_nonce=" + oath.generatenonce() +           "&oauth_timestamp=" + oath.generatetimestamp() +           "&oauth_consumer_key=" + consumerkey+           "&oauth_signature_method=plaintext" +           "&oauth_signature=" + consumersecret + "%26" + //%26 if plaintext           "&oauth_version=1.0" +           "&oauth_callback=" + "oob"; 
  1. using oauth_token of step 1. make request https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token={token}, returned me oauth_verifier

  2. using paramters of step 1 , 2 make request https://api.login.yahoo.com/oauth/v2/get_token , returned me new oauth_token , oauth_token_secret, oauth_session_handle, xoauth_yahoo_guid.

example url:

string sig = consumersecret + "%26" + oauthtokensecret; string url = "https://api.login.yahoo.com/oauth/v2/get_token?" +            "oauth_consumer_key=" + consumerkey+           "&oauth_signature_method=plaintext" +           "&oauth_signature=" + sig +           "&oauth_timestamp=" + oath.generatetimestamp() +           "&oauth_version=1.0" +           "&oauth_token=" + oauthtoken +           "&oauth_nonce=" + oath.generatenonce() +           "&oauth_verifier=" + oauth_verifier; 
  1. the final step contact list: make request https://social.yahooapis.com/v1/user/{xoauth_yahoo_guid}/contacts?format=json

example

uri uri = new uri(url); string nonce = oath.generatenonce(); string timestamp = oath.generatetimestamp(); string normalizedurl; string normalizedrequestparameters; string sig = oath.generatesignature(uri, clientid, clientsecret, yahootoken.oauthtoken,     yahootoken.oauthtokensecret, "get", timestamp, nonce, oauthbase.signaturetypes.hmacsha1,     out normalizedurl, out normalizedrequestparameters); 

function generatesignature:

public string generatesignature(uri url, string consumerkey, string consumersecret, string token, string tokensecret, string httpmethod, string timestamp, string nonce, signaturetypes signaturetype, out string normalizedurl, out string normalizedrequestparameters) {     normalizedurl = null;     normalizedrequestparameters = null;      switch (signaturetype)     {         case signaturetypes.plaintext:             return httputility.urlencode(string.format("{0}&{1}", consumersecret, tokensecret));         case signaturetypes.hmacsha1:             string signaturebase = generatesignaturebase(url, consumerkey, token, tokensecret, httpmethod, timestamp, nonce, hmacsha1signaturetype, out normalizedurl, out normalizedrequestparameters);              hmacsha1 hmacsha1 = new hmacsha1();             hmacsha1.key = encoding.ascii.getbytes(string.format("{0}&{1}", urlencode(consumersecret), string.isnullorempty(tokensecret) ? "" : urlencode(tokensecret)));              return generatesignatureusinghash(signaturebase, hmacsha1);         case signaturetypes.rsasha1:             throw new notimplementedexception();         default:             throw new argumentexception("unknown signature type", "signaturetype");     } } 

make request:

using (var client = new webclient()) {     string authenheader = "oauth " +         "realm=\"yahooapis.com\"" +         ",oauth_consumer_key=\"" + consumerkey+ "\"" +         ",oauth_nonce=\"" + nonce + "\"" +         ",oauth_signature_method=\"hmac-sha1\"" +         ",oauth_timestamp=\"" + timestamp + "\"" +         ",oauth_token=\"" + oauthtoken + "\"" +         ",oauth_version=\"1.0\"" +         ",oauth_signature=\"" + httputility.urlencode(sig) + "\"";      client.headers.set("authorization", authenheader);     string responsestring = client.downloadstring(url); } 

but yahoo sends me (401) unauthorized response, tell me i'm wrong ?

i realized stuck @ exact same place

system.net.webexception: remote server returned error: (401) unauthorized. @ ..getemailcontacts() in ...

  private arraylist getemailcontacts() {     oauthbase oauth = new oauthbase();      uri uri = new uri("https://social.yahooapis.com/v1/user/" + oauthyahooguid + "/contacts?format=xml");     string nonce = oauth.generatenonce();     string timestamp = oauth.generatetimestamp();     string normalizedurl;     string normalizedrequestparameters;     string sig = oauth.generatesignature(uri, this.sconsumerkey, this.sconsumersecret, oauthtoken, oauthtokensecret, "get", timestamp, nonce, oauthbase.signaturetypes.hmacsha1, out normalizedurl, out normalizedrequestparameters);      stringbuilder sbgetcontacts = new stringbuilder(uri.tostring());      try {     string returnstr = string.empty;     httpwebrequest req = (httpwebrequest)webrequest.create(sbgetcontacts.tostring());     req.accept = "application/xml";     req.method = "get";     arraylist emails = new arraylist();      string authheader = "authorization: oauth " +     "realm=\"yahooapis.com\"" +     ",oauth_consumer_key=\"" + this.sconsumerkey + "\"" +     ",oauth_nonce=\"" + nonce + "\"" +     ",oauth_signature_method=\"hmac-sha1\"" +     ",oauth_timestamp=\"" + timestamp + "\"" +     ",oauth_token=\"" + oauthtoken + "\"" +     ",oauth_version=\"1.0\"" +     ",oauth_signature=\"" + httputility.urlencode(sig) + "\"";      req.headers.add(authheader);      using (httpwebresponse res = (httpwebresponse)req.getresponse()) { 

:/ sighz


so found issue. ive changed content format xml json

changed following:

private arraylist getemailcontacts() {     oauthbase oauth = new oauthbase();      uri uri = new uri("https://social.yahooapis.com/v1/user/" + oauthyahooguid + "/contacts?format=json");     string nonce = oauth.generatenonce();     string timestamp = oauth.generatetimestamp();     string normalizedurl;     string normalizedrequestparameters;     string sig = oauth.generatesignature(uri, this.sconsumerkey, this.sconsumersecret, oauthtoken, oauthtokensecret, "get", timestamp, nonce, oauthbase.signaturetypes.hmacsha1, out normalizedurl, out normalizedrequestparameters);      stringbuilder sbgetcontacts = new stringbuilder(uri.tostring());      try {         string returnstr = string.empty;         httpwebrequest req = (httpwebrequest)webrequest.create(sbgetcontacts.tostring());         req.accept = "application/json";         req.contenttype = "application/json";         req.method = "get";         arraylist emails = new arraylist();          string authheader = "authorization: oauth " +         "realm=\"yahooapis.com\"" +         ",oauth_consumer_key=\"" + this.sconsumerkey + "\"" +         ",oauth_nonce=\"" + nonce + "\"" +         ",oauth_signature_method=\"hmac-sha1\"" +         ",oauth_timestamp=\"" + timestamp + "\"" +         ",oauth_token=\"" + oauthtoken + "\"" +         ",oauth_version=\"1.0\"" +         ",oauth_signature=\"" + httputility.urlencode(sig) + "\"";          req.headers.add(authheader);          using (httpwebresponse res = (httpwebresponse)req.getresponse()) { 

when made change returned emails , no more 406 error code.

i hope helps someone.. no idea why xml stopped working.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -