On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

Thursday, August 20, 2015

In January I wrote a post about how to publish posts automatically on Blogger.com. This however used for that the API v2.0, which is now deprecated, therefore I here want to show how to do this with the current API v3.0.
Unfortunately I did not find a way (and I do not know if there is one) to really publish the posts without the user having to interact, now he has to log in in the browser and copy some code.
From a high level view the procedure in this version is as follows:
Via the Google Developer Console a new project has to be created and the Blogger API has to be enabled. Information about this step and the general usage of the API can be found here.
Then, to be able to use the API, we need to authenticate via OAuth 2.0. For this, we have to send a request to Google. We can do so by calling a specific URL in the browser, as parameters we specify amongst others the ID of our project, the scope for which we want to use the access etc. The user then logs in in the browser and a authorization code is presented. This code the program then sends via an HTTP request to Google and we now get back a token. With this we can eventually call the API and thus publish posts on Blogger. This procedure is decribed here.

After this high level overview, let us come to concrete implementation: First we create a new project in the Google Developer Console. Then we look for the API Blogger API v3 in the menu APIs and enable it.
Now, in our C# program, we first have to call an URL to do our initial request. The needed URL is https://accounts.google.com/o/oauth2/auth. One parameter we pass over is scope, which describes for which application we want to authenticate, for this we send https://www.googleapis.com/auth/blogger. The next parameter is the redirect URL (redirect_uri), which determines to where the answer is send. When setting this to urn:ietf:wg:oauth:2.0:oob the answer is shown in the opened browser. Via response_type=code we determine to get a code back. As the last parameter we set client_id to the ID of our created project in the Google Developer Console. This is NOT the Project ID which can be found on the mainpage, but to get the client id one has to navigate to APIs & auth - Credentials and then click on (if not already done) Add credentials - OAuth 2.0 client ID. Then we select Other (because we are designing a native application) and click on Create - then we get the client id.
All in all the URL to be called should look like this:

We simply use Process.Start on the URL to start the default browser with it. In this, the user is presented with a login and consent screen. If he clicks accept, a success code is presented. We copy this.
With the code, we can get an access token for using the Blogger API. For this we now have to do a HTTP Post Request. The URL to be called is https://www.googleapis.com/oauth2/v3/token, as parameters we have to send the obtained code (code), the client id (client_id), the redirect url (redirect_uri, same as before), the grant type (grant_type=authorization_code - describes how we want to authenticate) and the client secret (client_secret). The latter we can see when clicking on the client id under Credentials.
With the method HTTPPost() presented in the linked post this looks as follows:

We read out the answer of the server since this contains (if successful) the access token. The answer is given in the JSON format, we use the library Newtonsoft.Jsonto interpret this. Maybe I will write a post about the library, for now I just refer to this post where it is also used.
Thus we obtain the access token via the following code, where AccessToken is a custom class with the desired attribute:

With this token we can now use the API to publish posts on Blogger. We use a WebRequest to send the correct POST request to the Blogger server. As the target adress we select https://www.googleapis.com/blogger/v3/blogs/.
First we set the correct content type, select the authentication header etc: