很难获得OAuth2.0令牌。继续获得400 Bad Request

时间:2014-03-28 08:18:03

标签: c# .net oauth-2.0 google-api

       /*WebClient client = new WebClient();

    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    string parameters = "code=4/f2PwqbN1Z5FpEEcT0scRH20B6d-F.ouqZIhzZqUYbEnp6UAPFm0EuJPwSigI&" +
        "client_id=162438320977-ml14ajmuutlrr71maal933ma5cjolc8l.apps.googleusercontent.com&" +
        "&client_secret=1IJxvSnmxcx-i2l_YGgYOD0i&redirect_uri=http://localhost&grant_type=authorization_code";

    byte[] data = Encoding.UTF8.GetBytes(parameters);
    var test = client.UploadData("http://accounts.google.com/o/oauth2/token","POST", data);
    string result = data.ToString();*/

    StringBuilder parameters = new StringBuilder();
    parameters.Append("code=4/IoAJMoYxUqk7lkH_WbSr3lk4URf1.0t7Qx7qZE5QeEnp6UAPFm0G0EvMSigI");
    parameters.Append("&client_id=162438320977-ml14ajmuutlrr71maal933ma5cjolc8l.apps.googleusercontent.com");
    parameters.Append("&client_secret=1IJxvSnmxcx-i2l_YGgYOD0i");
    parameters.Append("&redirect_uri=urn:ietf:wg:oauth:2.0:oob");
    parameters.Append("&grant_type=authorization_code");

    string p_params = parameters.ToString();
    byte[] p_data_params = Encoding.UTF8.GetBytes(p_params);

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create("https://accounts.google.com/o/oauth2/token");
    request.Accept = "application/json";
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = p_data_params.Length;

    Stream dataStream = request.GetRequestStream();
    dataStream.Write(p_data_params, 0, p_data_params.Length);
    dataStream.Close();

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
    string result = readStream.ReadToEnd();

似乎对我有用的唯一方法是使用HttpWebRequest。我在这里找到了答案:.Net Google OAuth token WebRequest Bad Request Protocol Error如果有人知道如何让Web客户端实现工作,请告诉我!感谢。

0 个答案:

没有答案