4

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");
4

1 回答 1

25

这就是我能够为我的请求设置正确的 OAuth auth 标头的方式:

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

构造函数的第一个参数是用于 Authorization 标头的方案。因此,在请求中,标头内容为:

Authorization: Bearer {the access token string}
于 2013-03-06T19:57:38.247 回答