zilzar / google-gdata

Automatically exported from code.google.com/p/google-gdata
0 stars 0 forks source link

OAuth2LeggedAuthenticator.ApplyAuthenticationToUri not working if original Uri contains query string #520

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Problem Description : OAuth2LeggedAuthenticator does not work if url contains 
query string.

Sample 2LeggedAuthenticator usage
e.g.
string url = "https://docs.google.com/feeds/default/private/full/" + resourceID;
DocumentQuery query = new DocumentQuery(url);
OAuth2LeggedAuthenticator auth = new OAuth2LeggedAuthenticator("My-GoogleApps", 
GoogleAppsOAuthConsumerKey, GoogleAppsOAuthConsumerSecret, googleUserName, 
googleDomain);
query.Uri = auth.ApplyAuthenticationToUri(new Uri(url));
Google.Documents.DocumentsRequest request = new 
Google.Documents.DocumentsRequest(requestSettings);
Feed<Document> feed2 = request.Get<Document>(query);
Document doc = feed2.Entries.First();

Source of bug :
Source file : authentication.cs

Line 491 : 
public override Uri ApplyAuthenticationToUri(Uri source)
        {
            UriBuilder builder = new UriBuilder(source);
            string queryToAppend = OAuth2LeggedAuthenticator.OAuthParameter + "=" + this.oAuthUser + "%40" + this.OAuthDomain;

            if (builder.Query != null && builder.Query.Length > 1)
                builder.Query = builder.Query + "&" + queryToAppend;   =======================> Line 497
            else
                builder.Query = queryToAppend;

            return builder.Uri;
        }

As per 
http://msdn.microsoft.com/en-us/library/system.uribuilder.query(v=VS.80).aspx 
(UriBuilder.Query Property)
"Do not append a string directly to this property. If the length of Query is 
greater than 1, retrieve the property value as a string, remove the leading 
question mark, append the new query string, and set the property with the 
combined string."
Can also refer to example given on MSDN link.

So instead of
builder.Query = builder.Query + "&" + queryToAppend;

should be
builder.Query = builder.Query.Substring(1) + "&" + queryToAppend; 

Corrected authentication.cs file attached.

Original issue reported on code.google.com by Mike.Lim...@gmail.com on 1 Jul 2011 at 8:57

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the patch, committed in rev. 1109

Original comment by ccherub...@google.com on 1 Jul 2011 at 12:22