0

401 error when trying to retrieve OAuth request token

Hi

I'm trying to create a sample C#/.NET app that would use Yahoo Contacts API. What I'm getting started with is retrieving request token. I've created a little program that does this (that's for a console app):

CODE
public static string GetWebRequestPOSTResult(string url, NameValueCollection coll) 
{
StringBuilder sbData = new StringBuilder();
for (int i = 0; i < coll.Count; i++)
{
if (sbData.Length > 0)
{
sbData.Append("&");
}
string key = coll.Keys[i];
sbData.Append("key=" + HttpUtility.UrlEncode(coll[key]));
}

return GetWebRequestPOSTResult(url, sbData.ToString());
}

public static string GetWebRequestPOSTResult(string url, string data)
{
string result = string.Empty;

Uri address = new Uri(url);
HttpWebRequest req = HttpWebRequest.Create(address) as HttpWebRequest;

req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";

byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
req.ContentLength = byteData.Length;

using (Stream postStream = req.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}

using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
result = reader.ReadToEnd();
}

return result;
}

static void Main()
{
DateTime startDate = new DateTime(1970, 1, 1, 0, 0, 0);

int timeStamp = (int)((DateTime.Now - startDate).TotalSeconds + 601);
string nonce = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 8);

System.Collections.Specialized.NameValueCollection appSettings = System.Configuration.ConfigurationManager.AppSettings;

string secretKey = appSettings["ydn_sharedsecret"];
string apiKey = appSettings["ydn_apikey"];
string appId = appSettings["ydn_appid"];

NameValueCollection coll = new NameValueCollection();
coll.Add("oauth_nonce", nonce);
coll.Add("oauth_timestamp", timeStamp.ToString());
coll.Add("oauth_consumer_key", apiKey);
coll.Add("oauth_signature_method", "HMAC-SHA1");
coll.Add("oauth_version", "1.0");
coll.Add("xoauth_lang_pref", "en-us");
coll.Add("oauth_callback", "http://test.desjalogic.com/YahooContactsHandler.aspx");

string result = GetWebRequestPOSTResult("https://api.login.yahoo.com/oauth/v2/get_request_token", coll);
Console.WriteLine(result);
}


The problem is that I always get exception which says: The remote server returned an error: (401) Unauthorized

I'm pretty sure that my authentication credentials that are stored in app.config file are correct. Does anybody know what the problem is?

by
9 Replies
  • the problem is the request string "https://api.login.yahoo.com/oauth/v2/.............................."
    0
  • Steven,

    I'm glad to see your reply, but could you please also give me a tip about what wrong with query string is? I would be very thankful for this
    0
  • I am working on the same thing, and I ll let you know when I got it work
    0
  • Just curious, are you using .NET?
    0
  • yes C# and VB.Net
    0
  • Hi,

    Can you please send the complete HTTP request and response headers
    you get so we can check?

    If you have access to tool such as wget (http://www.gnu.org/software/wget/)
    or curl (http://curl.haxx.se/), you can construct the post request
    manually with either of the following commands:

    wget -S -d https://api.login.yahoo.com/oauth/v2/get_request_token --post-data="oauth_version=1.0&oauth_nonce=8c76a87ffd3e4f48b53ed4b669b0b35f&oauth_timestamp=1268432350&oauth_consumer_key=dj0yJmk9TUJETmxhUG9WbDlPJmQ9WVdrOWR6Um9kRkl3TXpRbWNHbzlNV
    GszTVRBM05USXhOQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD02MQ--&oauth_signature_method=PLAINTEXT&oauth_signature=821ce9362da7d3d441265e77469a1339c42097612526&oauth_callback=oob"

    curl https://api.login.yahoo.com/oauth/v2/get_request_token -d "oauth_version=1.0&oauth_nonce=8c76a87ffd3e4f48b53ed4b669b0b35f&oauth_timestamp=1268432350&oauth_consumer_key=dj0yJmk9TUJETmxhUG9WbDlPJmQ9WVdrOWR6Um9kRkl3TXpRbWNHbzlNV
    GszTVRBM05USXhOQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD02MQ--&oauth_signature_method=PLAINTEXT&oauth_signature=821ce9362da7d3d441265e77469a1339c42097612526&oauth_callback=oob" -v

    Thanks,
    Yu Wang
    Yahoo! Membership Team
    0
  • that's what I get when I run curl with arguments you specified:

    c:\Projects\ curl>curl https://api.login.yahoo.com/oauth/v2/get_request_token -d
    "oauth_version=1.0&oauth_nonce=8c76a87ffd3e4f48b53ed4b669b0b35f&oauth_timestamp
    =1268432350&oauth_consumer_key=dj0yJmk9TUJETmxhUG9WbDlPJmQ9WVdrOWR6Um9kRkl3TXpRb
    WNHbzlNVGszTVRBM05USXhOQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD02MQ--&oauth_signature_meth
    od=PLAINTEXT&oauth_signature=821ce9362da7d3d441265e77469a1339c42097612526&oauth_
    callback=oob" -vwget -S -d https://api.login.yahoo.com/oauth/v2/get_request_toke
    n --post-data="oauth_version=1.0&oauth_nonce=8c76a87ffd3e4f48b53ed4b669b0b35f&o
    auth_timestamp=1268432350&oauth_consumer_key=dj0yJmk9TUJETmxhUG9WbDlPJmQ9WVdrOWR
    6Um9kRkl3TXpRbWNHbzlNVGszTVRBM05USXhOQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD02MQ--&oauth_
    signature_method=PLAINTEXT&oauth_signature=821ce9362da7d3d441265e77469a1339c4209
    7612526&oauth_callback=oob"
    curl: option --post-data=oauth_version=1.0&oauth_nonce=8c76a87ffd3e4f48b53ed4b66
    9b0b35f&oauth_timestamp=1268432350&oauth_consumer_key=dj0yJmk9TUJETmxhUG9WbDlPJm
    Q9WVdrOWR6Um9kRkl3TXpRbWNHbzlNVGszTVRBM05USXhOQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD02MQ
    --&oauth_signature_method=PLAINTEXT&oauth_signature=821ce9362da7d3d441265e77469a
    1339c42097612526&oauth_callback=oob: is unknown
    curl: try 'curl --help' or 'curl --manual' for more information

    Does it make any sense?
    0
  • this works for me: oAuth is from OAuthBase.cs


    Uri uri = new Uri("https://api.login.yahoo.com/oauth/v2/get_request_token");
    string nonce = oAuth.GenerateNonce();
    string timeStamp = oAuth.GenerateTimeStamp();
    string normalizeUrl, normalizedRequestParameters;

    StringBuilder sb = new StringBuilder(uri.ToString());

    int timestamp = UnixTime.ToUnixTime(DateTime.Now);


    sb.AppendFormat("?oauth_nonce={0}&", new GuidNonceProvider().GenerateNonce(timestamp));
    sb.AppendFormat("oauth_timestamp={0}&", timestamp.ToString(CultureInfo.InvariantCulture));
    sb.AppendFormat("oauth_consumer_key={0}&", apiKey);
    sb.AppendFormat("oauth_signature_method={0}&", "PLAINTEXT");
    sb.AppendFormat("oauth_signature={0}&", secret + "%26");
    sb.AppendFormat("oauth_version={0}&", "1.0");
    sb.AppendFormat("xoauth_lang_pref={0}&", "en-us");
    sb.AppendFormat("oauth_callback={0}", "oob");


    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sb.ToString());
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
    string responseBody = stream.ReadToEnd();
    stream.Close();
    response.Close();
    0
  • Steven, this is great. Thanks a lot!
    0

Recent Posts

in OAuth General Discussion YDN SDKs