HttpClient PostAsync to Blogger API

Stephen Hynes picture Stephen Hynes · Jan 31, 2013 · Viewed 11.3k times · Source

I am using the following API to allow me to interact with Google Blogger. I need to insert a post into the users blog. However I am having trouble with my PostAsync functionality. I get a 401 telling me that my request isn't authorized despite having an API Key, however I think I may not be properly inserting my OAuth token.

I have the following code,

This is the code where I set up my authorization header, (note the key there is fake but is the same form as what i think is the OAuth token)

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("ya29.AHES6ZTBZi1dWPVdlcF7qAD-nSM6XxwY2323232m4lXW");

And this is my PostAsync function

                HttpResponseMessage response = await req.PostAsync(URLs.postBlogURL + blogID + URLs.postBlogURLPost, new StringContent(json));

Can anyone tell me where I am going wrong? Cheers.

[UPDATE]

I amen't sure whether the authorization has to include the string bearer in it.

    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer ya29.AHES6ZTBZi1dWPVdlcF7qAD-nSM6XxwY2323232m4lXW");

Answer

Scott picture Scott · Mar 6, 2013

This is how I was able to get the proper OAuth auth header set for my request:

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", _accessTokenWrapper.Token.access_token );

The first parameter to the constructor is the scheme to use for the Authorization header. So in the request, the header reads:

Authorization: Bearer {the access token string}