简体   繁体   English

HttpClient PostAsync to Blogger API

[英]HttpClient PostAsync to Blogger API

I am using the following API to allow me to interact with Google Blogger. 我使用以下API来允许我与Google Blogger进行互动。 I need to insert a post into the users blog. 我需要在用户博客中插入帖子。 However I am having trouble with my PostAsync functionality. 但是我的PostAsync功能有问题。 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. 我得到一个401告诉我,尽管有API密钥,我的请求仍未获得授权,但我认为我可能无法正确插入我的OAuth令牌。

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) 这是我设置授权标题的代码,(注意密钥有假,但与我认为的OAuth令牌相同)

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

And this is my PostAsync function 这是我的PostAsync功能

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

This is how I was able to get the proper OAuth auth header set for my request: 这就是我能够为我的请求获取正确的OAuth auth标头集的方法:

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. 构造函数的第一个参数是用于Authorization标头的方案。 So in the request, the header reads: 所以在请求中,标题为:

Authorization: Bearer {the access token string}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM