Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private string HTTPPost(string url, string postparams)
{
string responseString = "";

// performs the desired http post request for the url and parameters
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// request.CookieContainer = Cookie; // explicitely use the cookiecontainer to save the session

string postData = postparams;
byte[] data = Encoding.UTF8.GetBytes(postData);

request.Method = "POST";
request.ContentType = "multipart/form-data";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}


var response = (HttpWebResponse)request.GetResponse();


responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

return responseString;

}

public class AccessToken
{
[JsonProperty(PropertyName = "access_token")]
public string access_token { get; set; }
}

private void Form1_Click(object sender, EventArgs e)
{
string Code = "code=" + "4/IivfhM7o5fd4fM6rLp6fHs1nhm5AeCIkO8iL_JcH9jg" + "&";
string ID = "client_id=29150183913-us8ansgqd27i0prnnch0576a93ahdi9f.apps.googleusercontent.com&";
string uri = "redirect_uri=urn:ietf:wg:oauth:2.0:oob&";
string grant = "grant_type=authorization_code&";
string secret = "client_secret=rsPLgZ-fD7_qHOTyqObxGE5O";

string Code2 = HTTPPost("https://www.googleapis.com/oauth2/v3/token", Code + ID + uri + grant + secret);

AccessToken JsonAccToken = (AccessToken)JsonConvert.DeserializeObject(Code2, typeof(AccessToken));
string StrAccToken = JsonAccToken.access_token;


try
{
JSONPublish("7962677485521194204", "Testpost", "This is a Test.", StrAccToken);
}
catch (Exception)
{

throw;
}
}

private void JSONPublish(string sid, string stitle, string scontent, string token)
{
var http = (HttpWebRequest)WebRequest.Create(new Uri("https://www.googleapis.com/blogger/v3/blogs/" + sid + "/posts/"));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
http.Headers.Add("Authorization", "Bearer " + token);

var vm = new { kind = "blogger#post", blog = new { id = sid }, title = stitle, content = scontent };
var dataString = JsonConvert.SerializeObject(vm);
string parsedContent = dataString;

Byte[] bytes = Encoding.UTF8.GetBytes(parsedContent);

Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();

var response = http.GetResponse();

var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
}







here is the Code I face Error at this line var response = (HttpWebResponse)request.GetResponse(); I face error of 400 bad request
I need Help how can I fix this..
And I don`t know what type of value given to him
C#
string uri = "redirect_uri=urn:ietf:wg:oauth:2.0:oob&";
         string grant = "grant_type=authorization_code&";
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900