Get Yahoo Contact List in C# application

痞子三分冷 提交于 2019-12-11 03:18:56

问题


As guidance on https://developer.yahoo.com/oauth/guide/oauth-requesttoken.html, Below are what I did with OAuth to get Yahoo contact list:

  1. Make a GET request to https://api.login.yahoo.com/oauth/v2/get_request_token to get oauth_token and oauth_token_secret, I successfully get 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. to make a GET request to https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token={token}, It returned me oauth_verifier

  2. Using paramters of step 1 and 2 to make GET request to https://api.login.yahoo.com/oauth/v2/get_token , it 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 is to get the contact list: I make a GET request to 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, Could you tell me where I'm wrong ?


回答1:


I realized that I am stuck at the exact same place

System.Net.WebException: The remote server returned an error: (401) Unauthorized. at ..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 I found the issue. Ive changed the content format from XML to JSON

changed the 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 I made that change it suddenly returned the emails and no more 406 error code.

I hope that this helps someone.. no idea as to why xml stopped working.



来源:https://stackoverflow.com/questions/31396439/get-yahoo-contact-list-in-c-sharp-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!