Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yahoo does use OAuth 1.0, so I figured out how to do it manually:</p> <p>First, I try to use the token I have saved in my database. If I get a 401 Unauthorized error, I call my RefreshYahooAccessToken() method, and try again.</p> <p>Note: I have my own implementation of a TokenManager that stores my access tokens in a database, as well as any extra data associated with the token (YahooGUID, and oauth_session_handle) which is given to me in Yahoo's get_token Response Parameters, but you can easily modify it to work with a different TokenManager implementation.</p> <p>Here's my working version:</p> <pre><code>try { request = yahoo.PrepareAuthorizedRequest(YahooContactsAPIEndpoint, TokenManager.currentToken.Token, extraData); response = yahoo.Channel.WebRequestHandler.GetResponse(request); body = response.GetResponseReader().ReadToEnd(); } catch (DotNetOpenAuth.Messaging.ProtocolException ex) { //is token expired? if (ex.InnerException is WebException &amp;&amp; ((WebException)ex.InnerException).Response is HttpWebResponse &amp;&amp; ((HttpWebResponse)((WebException)ex.InnerException).Response).StatusCode == HttpStatusCode.Unauthorized) { RefreshYahooAccessToken(); request = yahoo.PrepareAuthorizedRequest(YahooContactsAPIEndpoint, TokenManager.currentToken.Token, extraData); response = yahoo.Channel.WebRequestHandler.GetResponse(request); body = response.GetResponseReader().ReadToEnd(); } } private static void RefreshYahooAccessToken() { var request = (HttpWebRequest)WebRequest .Create("https://api.login.yahoo.com/oauth/v2/get_token" + "?oauth_consumer_key=" + TokenManager.ConsumerKey + "&amp;oauth_nonce=" + (new Random()).Next(123400, 9999999).ToString() + "&amp;oauth_session_handle=" + TokenManager.GetExtraData("oauth_session_handle") //this value is given to you in the get_token Response Parameters + "&amp;oauth_signature=" + TokenManager.ConsumerSecret + "%26" + TokenManager.currentToken.Secret + "&amp;oauth_signature_method=PLAINTEXT" + "&amp;oauth_timestamp=" + (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds.ToString().Split(new char[] { '.' })[0] + "&amp;oauth_token=" + TokenManager.currentToken.Token + "&amp;oauth_version=1.0"); try { var response = (HttpWebResponse)request.GetResponse(); var returnStr = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd(); var returnData = System.Web.HttpUtility.ParseQueryString(returnStr ?? string.Empty); TokenManager.ExpireRequestTokenAndStoreNewAccessToken(null, null, returnData["oauth_token"], returnData["oauth_token_secret"]); } catch (Exception) { //User probably revoked token. Clear the current token, and request authorization again } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload