0

I am implementing Yahoo OAuth 2.0 in C# and ASP.NET, what i have noticed that HttpUtility.UrlEncode(string) fails for the encoding of returnUrl. This is the case when the returnUrl has space in it. My code is -

    string consumerKey = "xxxxxxx";
    string returnUrl = "http://www.example.com/TutorialCode/Yahoo OAuth 2.0/yahoooauth2.aspx";
    /*Sending User To Authorize Access Page*/
    string url = "https://api.login.yahoo.com/oauth2/request_auth?client_id=" + consumerKey + "&redirect_uri=" + HttpUtility.UrlEncode(returnUrl) + "&response_type=code&language=en-us";
    Response.Redirect(url);

however if i change the returnUrl to-

string returnUrl = "http://www.example.com/TutorialCode/YahooOAuth2.0/yahoooauth2.aspx";

then it works. Why it is a problem in URL Encoding ? can somebody shed some light in this.

Regards

yogihosting
  • 5,494
  • 8
  • 47
  • 80

1 Answers1

0

The problem is not the URL encoding, but apparently that Yahoo refuses Redirect URI's with spaces (even if correctly URL-encoded). However, your URLs should not contain spaces in the first place as they are unsafe characters and must be URL encoded, see: Is a URL allowed to contain a space?. Note that in that case the redirect_uri value would become double URL encoded.

Community
  • 1
  • 1
Hans Z.
  • 50,496
  • 12
  • 102
  • 115
  • This answer is a bit confusing. You say that spaces should be url encoded, but the OP **is** url encoding the url. Do you mean the space character should be encoded first to %20, and then the whole thing urlencoded because it is being put inside of another url? If so, what utility methods should be used to accomplish the first part? – Phil Feb 01 '15 at 12:22
  • I meant to say that the the original value of `returnUrl` must not contain spaces. – Hans Z. Feb 01 '15 at 12:55
  • i have tried putting %20 for spaces but it did not worked. Yahoo documents does not say anything about url encoding the returnUrl. – yogihosting Feb 02 '15 at 05:35