سؤال

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;
    }
هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

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

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/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top