Tom Troughton
Tom Troughton

Reputation: 4325

Read public posts from Blogger using API

I have written some simple code for using my Twitter developer key and secret to read tweets from a public timeline. This is so I can display a user's latest tweets in their own website. The code is below.

I now wish to do something similar with their blog on Google's Blogger. I had assumed there would be a way to use my Google API key and secret to read the public content of a blog without the user needing to authenticate. I only need blog titles and dates so I can link through to the Blogger site. But I've spent 24 hours scouring the internet and cannot find any examples for getting an access token using just the key and secret.

Using the Google API SDK I have got as far as the code below but can't find a way to get the access token without getting the user to authenticate. Any advice appreciated - feel I'm banging my head against a wall! Happy to take any approach - I just want to get some Blogger content on a website I'm building...

Authenticate with Twitter:

        var oAuthConsumerKey = _key;
        var oAuthConsumerSecret = _secret;
        var oAuthUrl = "https://api.twitter.com/oauth2/token";

        // Do the Authenticate
        var authHeaderFormat = "Basic {0}";

        var authHeader = string.Format(authHeaderFormat,
             Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" +
                    Uri.EscapeDataString((oAuthConsumerSecret)))
                    ));

        var postBody = "grant_type=client_credentials";

        HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
        authRequest.Headers.Add("Authorization", authHeader);
        authRequest.Method = "POST";
        authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

        using (Stream stream = authRequest.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

        authRequest.Headers.Add("Accept-Encoding", "gzip");

        WebResponse authResponse = authRequest.GetResponse();

        // deserialize into an object
        string objectText;
        using (authResponse)
        {
            using (var reader = new StreamReader(authResponse.GetResponseStream())) 
            {
                objectText = reader.ReadToEnd();
            }
        }
        // objectText is JSON and contains access_token

Authenticate with Blogger??

    private bloggerTest()
    {
        // Register the authenticator.
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
        {
            ClientIdentifier = _key,
            ClientSecret = _secret
        };
        var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

        // Create the service.

        var service = new BloggerService(new BaseClientService.Initializer()
        {
            Authenticator = auth
        });

        var result = service.Posts.List(_blogID).Fetch();
    }

    private IAuthorizationState GetAuthorization(NativeApplicationClient arg)
    {
        var state = new AuthorizationState(new[] { BloggerService.Scopes.BloggerReadonly.GetStringValue() });
        // I am stuck here!
    }

Upvotes: 1

Views: 1169

Answers (1)

slugster
slugster

Reputation: 49984

For accessing a public feed it is way simpler than what you are doing. There are a bunch of classes built into the .Net framework for processing RSS feeds, you can start with SyndicationFeed. To get the feed items (blog posts) is quite simple:

XDocument feed = XDocument.Load(rssUrl);   //rssUrl is the URL to the rss page that (most) blogs publish
SyndicationFeed sf = SyndicationFeed.Load(feed.CreateReader());
foreach (SyndicationItem si in sf.Items)
{
    ..you've now got your feed item...
}

Note that this will give you the feed items but it won't give you the full web page that they appear in. (although you will get a URL for that).

Upvotes: 1

Related Questions