Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

c# - 401 Unauthorized using Yahoo OAuth

My below code returns a 401 Unauthorized error each time at the this point:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://query.yahooapis.com/v1/yql?q=select%20fields.value%20from%20social.contacts%20where%20guid%3Dme&diagnostics=false");
request.Headers.Add(
    "Authorization: OAuth " +
    "realm="" + "yahooapis.com" + ""," +
    "oauth_consumer_key="" + ConfigurationManager.AppSettings["yahoo_oauth_consumer_key"] + ""," +
    "oauth_nonce="" + Guid.NewGuid().ToString() + ""," +
    "oauth_signature_method="" + "PLAINTEXT" + ""," +
    "oauth_timestamp="" + ((DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1).Ticks) / (1000 * 10000)).ToString() + ""," +
    "oauth_token="" + accessToken.TokenValue + ""," +
    "oauth_version="1.0" + ""," +
    "oauth_signature="" + ConfigurationManager.AppSettings["yahoo_oauth_signature"] + "%26" + """
);
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream(), true);

And when I step into the code everytime I see all the info is there that Yahoo requests to have in the header, yet everytime I get this 401.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As I'm sure you know, 401 means not authorised. It probably means that your signature is not valid.

You don't post all of your code but I can't see how it could possibly correctly generate the correct signature, since your signature method seems not to take any of the parameters -- most tellingly the timestamp and nonce -- as input. If you've already calculated the signature correctly, then you need to use the exact same timestamp and nonce in the Authorized header; using a different one will mean that the signature is not valid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...