我尝试了下面的代码,在向Google博客发布新的Feed时返回了Unauthorized异常。
var http = (HttpWebRequest)WebRequest.Create(
new Uri("https://www.googleapis.com/blogger/v3/blogs/4310477689444728962/posts"));
http.Credentials = new NetworkCredential("XXXXXXXX", "XXXXXXX");
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
http.Headers[HttpRequestHeader.Authorization]
= "Bearer" + "AIzaSyC-o1Y7Oluso7SrZuoy3JjuRfDHsoCEEsg";
var vm = new {
kind = "blogger#post",
blog = new { id = "4310477689444728962" },
title = "Testtitle", content = "testcontent"
};
var dataString = JsonConvert.SerializeObject(vm);
UTF8Encoding encoding = new UTF8Encoding();
string parsedContent = dataString;
//ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes1 = encoding.GetBytes(parsedContent);
Stream newStream =await http.GetRequestStreamAsync();
newStream.Write(bytes1, 0, bytes1.Length);
newStream.Dispose();
var response =await http.GetResponseAsync();
var stream = response.GetResponseStream();
var sr1 = new StreamReader(stream);
var content = sr1.ReadToEnd();