0

I have been accessing google reader in C# code for quite some time using ClientLogin authentication. I have been working on a Google Reader client for Windows 8 and decided to use OAuth 2 authentication which is more secure. I can login, get access token and refresh token, But I have failed to use the access token to make any api calls getting a 401 Unauthorized error. And my token comes with many characters starting with "ya29.". I hope the token is right. I am using the HttpClient class while passing the "Authorization:" header to it. Could someone please help me out? Below is my code snippet

private async Task<string> HttpGet()
    {
    string url = "http://www.google.com/reader/api/0/subscription/list";
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", 
            String.Format("OAuth access_token={0}", Token);

        var response = await client.GetAsync(url);
        var content = await response.Content.ReadAsStringAsync();
        return content;
    }

3 Answers3

1

It seems you are not using the correct Authorization header format. According to https://developers.google.com/accounts/docs/OAuth2WebServer#callinganapi, the header should be:

Authorization: Bearer ya29.xxxxxxxxxxxx
niuchl
  • 41
  • 2
0

I recommend using RestSharp. It's a lot easier to use than HttpClient and in addition has support for OAuth 1 & 2. Here's a tutorial on how to use it:

http://www.codeproject.com/Articles/321291/Google-OAuth2-on-Windows-Phone

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
  • Restsharp doesn't seem to support Windows RT. Still looking for alternatives. So is it that HttpClient doesn't support Oauth 2 completely? – Sume Rossini Jan 10 '13 at 09:56
  • Thank you very much brother. I managed to use a forked version of RestSharp by Devinrader. Its crude, but still worked like a charm. Am hating WinRT. Its like all of a sudden all my knowledge in .NET has become useless n I have to learn stuff from scratch – Sume Rossini Jan 10 '13 at 10:39
0

May I make a suggestion?

I hate writing oauth code.

The most simplistic way to add Google authentication to your application is to use Windows Azure Mobile Services (WAMS). WAMS will handle the oauth details for you through their SDK, which also enables Live ID, FaceBook and Twitter. It is C# and JavaScript, Android, and iOS. Best of all, it is free.

cite: http://www.windowsazure.com/en-us/home/features/mobile-services/

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233