Search code examples
c#google-oauth

Given path's format not supported when using Google's OAuth2 library


I've been trying to add authentication to a Google service via OAuth2 to a desktop (C#, WPF) application I'm working on. I'm using the OAuth 2.0 C# libraries provided by Google via nuGet, and I've modeled my code on the example on Google's site. My login code looks like this:

Task<UserCredential> loginTask = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = ClientId, ClientSecret = ClientSecret },
new[] { BloggerService.Scope.Blogger },
cmbUser.SelectedItem.ToString(),
CancellationToken.None,
new FileDataStore("Blogger.Auth"));

Whenever I execute that code, my browser comes up and asks me to login to Google. But, after doing that, that line throws a System.AggregateException. Digging into the inner exception, it looks like the core problem is this:

InnerException: System.NotSupportedException
   HResult=-2146233067
   Message=The given path's format is not supported.
   Source=Microsoft.Threading.Tasks
   StackTrace:
        at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
        at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
        at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
        at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
        at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Auth.DotNet4\OAuth2\GoogleWebAuthorizationBroker.cs:line 59
   InnerException: 

I've tried searching the web, but I haven't seen anyone running across this problem with OAuth2 before. Does anyone have any idea what could be causing this error and how to resolve it?

A few more details--my application is using the .NET framework 4.5. For authorization, I'm using version 1.9 of Google.Apis.Auth.


Solution

  • I figured out the problem. For the username, I was using cmbUser.SelectedItem.ToString(). That combo box had a bunch ListBoxItems holding email addresses, and when ToString() was called on one of those, a value like this would be returned:

    "System.Windows.Controls.ListBoxItem: [email protected]"
    

    That string has characters that were not valid in a path, so I was getting the invalid path exception.

    I was able to fix this by getting the content property of the ListBoxItem, which just returned the email address.